Python 异步事件循环。 运行 完成异步任务后永远循环
Python asyncio event loop. Run loop forever after completing an async task
好吧,我是 python 中的异步新手。我正在使用调用 asyncio.start_server
创建服务器,问题是我 运行 两次使用相同的循环,第一次是 create/start 服务器调用 loop.run_until_complete
,然后是 loop.run_forever
。这是我使用的代码。
if __name__ == '__main__':
loop = asyncio.get_event_loop()
sv_wrapper = ServerWrapper(
host='localhost',
port=5003
)
loop.run_until_complete(sv_wrapper.create())
print(repr(sv_wrapper.server))
loop.run_forever()
(Full code example)
老实说,我没有得到对 loop.run_forever()
的最后一次调用, 是否在执行调用的同一事件循环或新事件循环中使用 asyncio.start_server
运行 de 创建了服务器是在内部创建的?
如果在内部创建了一个新的事件循环,我不需要永远调用 运行,例如只保留进程 运行ning 可能就足够了(当然还有对创建的 Server 的引用)。
我不知道这是否有意义,但如果服务器是一个循环本身(管理 in/out 即将到来的连接作为未来的任务)是否可以使用 loop.create_task
推送任务?
我没有遇到具体问题,很抱歉那。我来自 nodejs 背景,我认为在 python 中获取异步会更容易,感谢您的帮助,任何额外内容都会受到欢迎!
你为什么不直接查看源代码呢?
try:
events._set_running_loop(self)
while True: # Here is the point.
self._run_once() # Run event loop once.
if self._stopping: # Check stop
break # Stop event loop.
finally:
self._stopping = False
self._thread_id = None
events._set_running_loop(None)
self._set_coroutine_wrapper(False)
if self._asyncgens is not None:
sys.set_asyncgen_hooks(*old_agen_hooks)
这是 run_forever
的一部分。你可以看到,不调用 run_forever
,你甚至不会 运行 任何任务。
Honestly I do not get the last call to loop.run_forever(), does de
created server with asyncio.start_server run on the same event loop
that executes the call, or a new event loop is created internally?
它是单个全局事件循环。通常最终用户管理创建和 运行 宁事件循环,库不在内部进行。
If a new event loop is created internally, I do not need the call to
run forever, for example just keeping the process running could be
enough (and of course having a reference to the created Server).
我不确定我是否理解你的意思,但这里有一些想法:
您的服务器只能在事件循环处于 运行ning 时工作。服务器只能通过事件循环接收或发送内容。
loop.run_until_complete(sv_wrapper.create())
表示该事件
循环用于执行一个作业(创建服务器)然后被停止。
同样,这意味着您应该 运行 它才能使创建的服务器正常工作。
I do not know if this have sense at all, but if the server is a loop
itself (manage in/out coming connections as future tasks) Is it
possible to push tasks with loop.create_task?
服务器本身不是事件循环。粗略地说,服务器是由全局事件循环管理的异步任务之一。
您可以创建其他异步任务( 与 ensure_future
一起完成),这些任务将由相同的全局事件循环管理(并通过 运行 与服务器同时进行) .
好吧,我是 python 中的异步新手。我正在使用调用 asyncio.start_server
创建服务器,问题是我 运行 两次使用相同的循环,第一次是 create/start 服务器调用 loop.run_until_complete
,然后是 loop.run_forever
。这是我使用的代码。
if __name__ == '__main__':
loop = asyncio.get_event_loop()
sv_wrapper = ServerWrapper(
host='localhost',
port=5003
)
loop.run_until_complete(sv_wrapper.create())
print(repr(sv_wrapper.server))
loop.run_forever()
(Full code example)
老实说,我没有得到对 loop.run_forever()
的最后一次调用, 是否在执行调用的同一事件循环或新事件循环中使用 asyncio.start_server
运行 de 创建了服务器是在内部创建的?
如果在内部创建了一个新的事件循环,我不需要永远调用 运行,例如只保留进程 运行ning 可能就足够了(当然还有对创建的 Server 的引用)。
我不知道这是否有意义,但如果服务器是一个循环本身(管理 in/out 即将到来的连接作为未来的任务)是否可以使用 loop.create_task
推送任务?
我没有遇到具体问题,很抱歉那。我来自 nodejs 背景,我认为在 python 中获取异步会更容易,感谢您的帮助,任何额外内容都会受到欢迎!
你为什么不直接查看源代码呢?
try:
events._set_running_loop(self)
while True: # Here is the point.
self._run_once() # Run event loop once.
if self._stopping: # Check stop
break # Stop event loop.
finally:
self._stopping = False
self._thread_id = None
events._set_running_loop(None)
self._set_coroutine_wrapper(False)
if self._asyncgens is not None:
sys.set_asyncgen_hooks(*old_agen_hooks)
这是 run_forever
的一部分。你可以看到,不调用 run_forever
,你甚至不会 运行 任何任务。
Honestly I do not get the last call to loop.run_forever(), does de created server with asyncio.start_server run on the same event loop that executes the call, or a new event loop is created internally?
它是单个全局事件循环。通常最终用户管理创建和 运行 宁事件循环,库不在内部进行。
If a new event loop is created internally, I do not need the call to run forever, for example just keeping the process running could be enough (and of course having a reference to the created Server).
我不确定我是否理解你的意思,但这里有一些想法:
您的服务器只能在事件循环处于 运行ning 时工作。服务器只能通过事件循环接收或发送内容。
loop.run_until_complete(sv_wrapper.create())
表示该事件 循环用于执行一个作业(创建服务器)然后被停止。 同样,这意味着您应该 运行 它才能使创建的服务器正常工作。
I do not know if this have sense at all, but if the server is a loop itself (manage in/out coming connections as future tasks) Is it possible to push tasks with loop.create_task?
服务器本身不是事件循环。粗略地说,服务器是由全局事件循环管理的异步任务之一。
您可以创建其他异步任务(ensure_future
一起完成),这些任务将由相同的全局事件循环管理(并通过 运行 与服务器同时进行) .