datawhalechina / self-llm

《开源大模型食用指南》基于Linux环境快速部署开源大模型,更适合中国宝宝的部署教程
Apache License 2.0
6.14k stars 753 forks source link

报错asyncio.run() cannot be called from a running event loop,辛苦大佬们看看 #97

Closed chaoskklt closed 3 weeks ago

chaoskklt commented 2 months ago

报错 asyncio.run() cannot be called from a running event loop 再autodl上进行配置的


日志如下:

RuntimeError Traceback (most recent call last) Cell In[2], line 89 85 model = AutoModelForCausalLM.from_pretrained(model_name_or_path, device_map="auto", torch_dtype=torch.bfloat16).cuda() 87 # 启动FastAPI应用 88 # 用6006端口可以将autodl的端口映射到本地,从而在本地使用api ---> 89 uvicorn.run(app, host='0.0.0.0', port=6006, workers=1) # 在指定端口和主机上启动应用

File ~/miniconda3/lib/python3.10/site-packages/uvicorn/main.py:575, in run(app, host, port, uds, fd, loop, http, ws, ws_max_size, ws_max_queue, ws_ping_interval, ws_ping_timeout, ws_per_message_deflate, lifespan, interface, reload, reload_dirs, reload_includes, reload_excludes, reload_delay, workers, env_file, log_config, log_level, access_log, proxy_headers, server_header, date_header, forwarded_allow_ips, root_path, limit_concurrency, backlog, limit_max_requests, timeout_keep_alive, timeout_graceful_shutdown, ssl_keyfile, ssl_certfile, ssl_keyfile_password, ssl_version, ssl_cert_reqs, ssl_ca_certs, ssl_ciphers, headers, use_colors, app_dir, factory, h11_max_incomplete_event_size) 573 Multiprocess(config, target=server.run, sockets=[sock]).run() 574 else: --> 575 server.run() 576 if config.uds and os.path.exists(config.uds): 577 os.remove(config.uds) # pragma: py-win32

File ~/miniconda3/lib/python3.10/site-packages/uvicorn/server.py:65, in Server.run(self, sockets) 63 def run(self, sockets: list[socket.socket] | None = None) -> None: 64 self.config.setup_event_loop() ---> 65 return asyncio.run(self.serve(sockets=sockets))

File ~/miniconda3/lib/python3.10/asyncio/runners.py:33, in run(main, debug) 9 """Execute the coroutine and return the result. 10 11 This function runs the passed coroutine, taking care of (...) 30 asyncio.run(main()) 31 """ 32 if events._get_running_loop() is not None: ---> 33 raise RuntimeError( 34 "asyncio.run() cannot be called from a running event loop") 36 if not coroutines.iscoroutine(main): 37 raise ValueError("a coroutine was expected, got {!r}".format(main))

RuntimeError: asyncio.run() cannot be called from a running event loop

KMnO4-zx commented 2 months ago

fastapi 不要在jupyter notebook里运行

chaoskklt commented 2 months ago

谢谢大佬深夜回答,我是小白。我在autodl只找到用jupyter notebook 编写和运行代码的地方。大佬能否再细致讲解一下

KMnO4-zx commented 2 months ago

在左侧界面 右键“创建文件” xxx.py 你可以自己摸索摸索

chaoskklt commented 2 months ago

谢谢

KMnO4-zx commented 2 months ago
image
107350qin commented 2 months ago

fastapi启动之后本身系统就在一个事件循环中,你不能再创建其他事件了。 你可以把 uvicorn.run(app, host='0.0.0.0', port=6006, workers=1) 注释掉,使用 asyncio.run(hello()) 来运行指定方法