----My env:
Win10, python 3.9 in anaconda,sanic 22.12.0
----My simple code:
import json
import sanic
app_train = sanic.Sanic(name='modelTrain')
@app_train.route('/lalian', methods=['POST', 'GET'])
def serverListen( request):
""" fetch body:json parse """
recv_info = json.loads(request.body.decode('utf-8'))
print(recv_info)
temp_info = {'value': 1}
print(temp_info)
return sanic.response.json(temp_info)
if __name__ == '__main__'(showing problem with underline here):
sv_host,sv_port = '127.0.0.1',8009
print('Start server... ')
app_train.run(host=sv_host, port=sv_port, workers=1, debug=False)
----Contrast ways
----1.The code as above could start and run normally in pycharm(OK):
----2.The code coud start and run normally with py file in command line(OK):
----3.After I package it into exe: pyinstaller -D test.py,it runs and restarts in infinite loop as follow(NOK):
Problem 3 troubled me for several days,and I have to package it into exe.
I tried adding code ahead as follow:
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
And these codes in
if __name__ == '__main__'
(showing problem with underline here):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
sv_host,sv_port = '127.0.0.1',8009
app_train.run(host=sv_host, port=sv_port, workers=1, debug=False)
But there was no use…
I hope for your help to solve it,thanks in advance!