简体   繁体   English

我怎样才能在另一个功能旁边运行 websockets 服务器?

[英]How could I run a websockets server alongside another function?

I want to run a websockets server alongside another function in my program, however when I try to use asyncio.gather() the other function runs fine while the websockets server closes instantly, I think because I cannot use loop.run_forever() .我想在我的程序中与另一个函数一起运行websockets服务器,但是当我尝试使用asyncio.gather() ,另一个函数运行良好,而 websockets 服务器立即关闭,我想是因为我不能使用loop.run_forever() My code is as follows:我的代码如下:

import asyncio
import websockets

from server import server, game_controller

loop = asyncio.get_event_loop()
tasks = asyncio.gather(
    websockets.serve(server, 'localhost', 5000),
    game_controller()
)

try:
    loop.run_until_complete(tasks)
finally:
    loop.close()

Putting loop.run_forever() there when using run_until_complete(...server...) works fine, but it doesn't work with gather .使用run_until_complete(...server...)时将loop.run_forever()放在那里工作正常,但它不适用于gather I'm fairly new to asyncio, so I'm not sure how I could go about fixing this.我对 asyncio 还很陌生,所以我不确定如何解决这个问题。

发现问题,我忘了让game_controller()函数async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM