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

Tim Allen screwtape at froup.com
Mon Aug 27 22:47:50 MDT 2018


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




More information about the Twisted-Python mailing list