On Wed, Jan 29, 2014 at 9:36 AM, Victor Stinner <[email protected]>wrote:

> I'm still to trying to figure out why the main thread is a special
> case in Tulip. For example, EventLoopPolicy.get_event_loop() only
> creates a new event loop when it is called from the main thread. Is it
> to avoid bugs?
>

A confused user who has an existing multi-threaded program and wants to
start integrating asyncio can easily get themselves in an undesirable
situation if we were to create default event loops for all threads, e.g.
creating a Future in one thread and trying to wait for it in another. This
could even happen when submitting some code to run in a separate thread
using run_in_executor() and that code somehow starts to use Futures.


> Does it mean that I need to call explicitly policy.new_event_loop()
> and then policy.set_event_loop(loop) if I want to use a loop in a
> thread different than the main thread?
>

Right. But first you have to think deep about whether that is really what
you need.


> _UnixDefaultEventLoopPolicy._init_watcher() does nothing if it is
> called from a thread different than the main thread. Does it mean that
> it is not possible to run an event loop running subprocesses from a
> thread different than the main thread?
>

IIRC the watcher logic distributes signals to other event loops that
register, so as long as you have a watcher running in the main thread you
should be able to use subprocesses in event loops running in other threads.
I've CC'ed the author of that code, Anthony Baire; hopefully he can
enlighten you.


> I can understand that only one event loop can install an handler sor
> SIGCHLD signal, but why not allowing to run it in a different thread?
>

Signals always get delivered to the main thread.

I just modified get_child_watcher() to add an optional loop parameter
> in my subprocess_stream branch, so it's possible to run unit tests
> with set_event_loop(None).
>
>
> http://code.google.com/p/tulip/source/detail?r=f9e077be6d3b97fde52b10131224d75696a0cbdf&name=subprocess_stream
>
> Victor
>

I think Anthony should review that.

-- 
--Guido van Rossum (python.org/~guido)

Reply via email to