Hope for great help:Appear infinite loop after run the sanic exe which is packaged by pyinstaller

----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'))
    temp_info = {'value': 1}
    return sanic.response.json(temp_info)
if __name__ == '__main__'(showing problem with underline here):
    sv_host,sv_port = '',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:


And these codes in

if __name__ == '__main__'(showing problem with underline here):

loop = asyncio.new_event_loop()
sv_host,sv_port = '',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!

I do not know enough about pyinstaller, and cannot help you with its configurations. What I can say is that sanic will take care of setting the loop for you so you shouldn’t need to do that. I’ve never once had luck packaging anything more than a hello world app with pyinstaller, so I’m not the person to ask. I suggest you seek out their help, as this is truly a bizarre behavior localized to this implementation. Best of luck :sunglasses: