将编写的Sanic程序导入另一个程序后发生的“上下文已设置”错误,如何解决?

  • python version: 3.10
  • Sanic-Cors version: 2.2.0
  • sanic version: 24.6.0
  • OS: Win 11
    首先我尝试直接运行了api.py,没有发生任何错误,但是当我再尝试运行main.py的时候,发生了:
    

    PS E:\project>python main.py
    Main  2024-08-15 21:58:44 +0800 INFO: Sanic v24.6.0
    Main  2024-08-15 21:58:44 +0800 INFO: Goin' Fast @ http://127.0.0.1:5050
    Main  2024-08-15 21:58:44 +0800 INFO: app: ip
    Main  2024-08-15 21:58:44 +0800 INFO: mode: production, single worker
    Main  2024-08-15 21:58:44 +0800 INFO: server: sanic, HTTP/1.1
    Main  2024-08-15 21:58:44 +0800 INFO: python: 3.12.4
    Main  2024-08-15 21:58:44 +0800 INFO: platform: Windows-11-10.0.22631-SP0
    Main  2024-08-15 21:58:44 +0800 INFO: packages: sanic-routing==23.12.0
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "xxx\Python\Python312\Lib\multiprocessing\spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "xxx\Python\Python312\Lib\multiprocessing\spawn.py", line 131, in _main
    prepare(preparation_data)
    File "xxx\Python\Python312\Lib\multiprocessing\spawn.py", line 246, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
    File "xxx \Python\Python312\Lib\multiprocessing\spawn.py", line 297, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "<frozen runpy>", line 286, in run_path
    File "<frozen runpy>", line 98, in _run_module_code
    File "<frozen runpy>", line 88, in _run_code
    File "xxx\test\main.py", line 3, in <module>
    api.start_server()
    File "xxx\test\api.py", line 16, in start_server
    app.run(host="127.0.0.1", port=5050)
    File "xxx\Python\Python312\Lib\site-packages\sanic\mixins\startup.py", line 291, in run
    serve(primary=self)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^
    File "xxx\Python\Python312\Lib\site-packages\sanic\mixins\startup.py", line 1003, in serve
    cls._set_startup_method()
    File "xxx\Python\Python312\Lib\site-packages\sanic\mixins\startup.py", line 932, in _set_startup_method
    set_start_method(method, force=cls.test_mode)
    File "xxx\Python\Python312\Lib\multiprocessing\context.py", line 247, in set_start_method
    raise RuntimeError('context has already been set')
    RuntimeError: context has already been set

    
    这是第一个python文件 (api.py):
    

    # api.py
    from sanic import Sanic, json
    from sanic_cors import CORS
    
    app = Sanic("ip")
    CORS(app)
    
    
    @app.route("/get_proxy")
    async def ip_api(request):
      ip = "XXXXXX"
      return json({"ip": ip})
    
    
    def start_server():
      app.run(host="127.0.0.1", port=5050)
    
    
    if __name__ == '__main__':
      start_server()

    
    这是第二个python文件 (main.py):
    

    # main.py
    import api
    
    api.start_server()

    
    感谢您提供的帮助。
    

我原本怀疑时主模块检查因为某些原因自己运行了,于是删除了那段代码,但并没有任何效果。

阅读 1.1k
1 个回答
新手上路,请多包涵

感谢各位,本贴已于 Stackoverflow 上解决,这是解决方法,希望能帮助到更多朋友。

This has something to do with the fact that Sanic has to start in main, but the routes can be defined anywhere else.
Sanic 必须在__main__中启动,因为Sanic需要在其中做一些事情,但是相关路由可以定义在任何地方。

This works:
这样做就可以正常运行了:

# api.py
from sanic import Sanic, json
from sanic_cors import CORS

app = Sanic("ip")
CORS(app)


@app.route("/get_proxy")
async def ip_api(request):
    ip = "XXXXXX"
    return json({"ip": ip})

if __name__ == '__main__':
    app.run(host="127.0.0.1", port=5050)

Or try this:
或者试试这样做:

# main.py
from api import start_server

if __name__ == "__main__":
    start_server()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏