7

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.

How is it even possible to run subprocess from different threads if event loop must run in the main thread?

1 Answer 1

12

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.

After that you may create subprocess from non-main thread.

UPD

Starting from Python 3.8 asyncio has no limitations mentioned above.

Everything just works.

Sign up to request clarification or add additional context in comments.

5 Comments

Why do we need this limitation anyway? I would like to avoid occupying main thread, since it's already busy with GUI s event look (Qt).
When asyncio starts subprocess it need to be notified by subproc finish event. Unfortunately in Unix systems the generic way to do it is catching SIG_CHLD signal. Python interpreter can process signals only in main thread. BTW you can try to use Quamash for running asyncio eventloop over Qt.
I will, thank you for suggestion. Wouldn't it possible to make signal catching implicit, without direct requirement of event_loop?
Nope, sorry. Implicit signal catching cannot be implemented without deep enough asyncio rewriting. Moreover, signals behave like singletons: every part of your program want to have exclusive access to signal system, but libraries may conflict each other.
What does UPD mean in this case?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.