简体   繁体   English

asyncio是否支持从非主线程运行子进程?

[英]Does asyncio support running a subprocess from a non-main thread?

I'm developing an application that mainly consists of services which are threads with custom run loops. 我正在开发一个主要由服务组成的应用程序,这些服务是具有自定义运行循环的线程。

One of the services needs to spawn subprocesses and I don't really understand whether it's valid or not. 其中一个服务需要产生子进程,我真的不明白它是否有效。 Official documentation is ambiguous. 官方文件含糊不清。 Namely it says both asyncio supports running subprocesses from different threads and An event loop must run in the main thread in the same section. 即它表示asyncio supports running subprocesses from different threads An event loop must run in the main thread ,并且An event loop must run in the main thread在同一部分An event loop must run in the main threadAn event loop must run in the main thread

How is it even possible to run subprocess from different threads if event loop must run in the main thread? 如果事件循环必须在主线程中运行,那么甚至可以从不同的线程运行子进程?

Documentation says: 文件说:

  1. You should have running event loop in the main thread. 您应该在主线程中运行事件循环。
  2. In the main thread please call asyncio.get_child_watcher() at the start of the program. 在主线程中,请在程序开头调用asyncio.get_child_watcher()

After that you may create subprocess from non-main thread. 之后,您可以从非主线程创建子进程。

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

相关问题 在非主线程中显示对话框 - Showing dialog in non-main thread 主线程中对queue.join()的调用对非主线程有什么影响? - What does a call on queue.join() in main thread do to non-main threads? QApplication 在 pyQt4 的第二个进程的非主线程中:这段代码是否合法,如果不合法,为什么它有效? - QApplication in a non-main thread of second process with pyQt4: is this code legal, and if not, why does it work? 如何在非主线程中创建GUI小部件 - How to create GUI widget in a non-main thread python matplotlib:在非主线程中绘制到 GUI - python matplotlib: Plotting to GUI in non-main thread Python - 在一个单独的子进程或线程中运行Autobahn | Python asyncio websocket服务器 - Python - Running Autobahn|Python asyncio websocket server in a separate subprocess or thread Python asyncio:在辅助线程上运行subprocess_exec - Python asyncio: Running subprocess_exec on a worker thread 如何在非主线程上运行 pyttsx3(文本到语音)? (Python)[已回答] - How to run pyttsx3 (text-to-speech) on non-main thread? (Python) [Answered] TIS / TSM非主线程错误;热键触发的pygame脚本(臀部,pygame,键盘) - TIS/TSM non-main thread error; pygame script triggered by hotkey (rumps, pygame, keyboard) 无法使用非主线程在Python-OpenCV(cv2)中启动相机捕获 - Unable to start camera capture in Python-OpenCV (cv2) using a non-main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM