[Twisted-Python] Forking after starting AsyncioSelectorReactor: Supported?

David Foster davidfstr at gmail.com
Tue Aug 28 08:49:46 MDT 2018


Thank you for the detailed research Tim.

Indeed it seems we’ll have to either change the order of {start reactor, fork} or use a different reactor type, since this problem seems likely to remain so long as asyncio uses the kqueue primitive on macOS, which seems unlikely to change.

- David

> On Aug 27, 2018, at 9:47 PM, Tim Allen <screwtape at froup.com> wrote:
> 
>> On Mon, Aug 27, 2018 at 09:12:57PM -0700, David Foster wrote:
>> So my question is, does Twisted support being forked after starting a
>> reactor or not?
> 
> I haven't used Twisted with the AsyncioSelectorReactor on macOS myself, but
> the `asyncio` docs suggest[1] that the default macOS event loop uses the
> `kqueue` system call. Searching for "macos fork kqueue" finds a report[2]
> of the same behaviour in the C++ Boost asyncio library. Apple doesn't seem
> to publish manpages publically anymore, but the `kqueue` system call was
> borrowed from FreeBSD which does[3]:
> 
> # The kqueue() system call creates a new kernel event queue and returns a
> # descriptor. The queue is not inherited by a child created with fork(2).
> 
> So, my guess is that `kqueue` just can't be used with `fork` in that way.
> If you really need to set up a reactor and then fork, perhaps you
> can configure `asyncio` to use the `selectors.SelectSelector` or
> `selectors.PollSelector` event loops instead; they're less efficient,
> but they should work after a fork.
> 
> This behaviour doesn't occur on Ubuntu because Linux provides the `epoll`
> system call instead of `kqueue`, which behaves differently.
> 
> [1]: https://docs.python.org/3.6/library/asyncio-eventloops.html#mac-os-x
> [2]: https://svn.boost.org/trac10/ticket/3238
> [3]: https://www.freebsd.org/cgi/man.cgi?query=kqueue&manpath=FreeBSD+11.2-RELEASE+and+Ports
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list