Opened 22 months ago

Last modified 22 months ago

#6147 enhancement new

public API for initializing the reactor when a main loop is already running

Reported by: glyph Owned by:
Priority: normal Milestone:
Component: core Keywords:
Cc: Branch:
Author: Launchpad Bug:


Sometimes, you run the loop, and sometimes, the loop runs you.

Specifically, sometimes you are writing a full program with a main() and everything, and sometimes you are writing a plugin for a program that is already running a main loop; many GUI programs, especially, support Python plugins.

It should be possible to use Twisted from within a program that is already using a supported main loop (i.e. GTK, CF, QT). Implementation wise, this is usually quite a simple proposition: for example, in _glibbase, this could be easily made to work by moving the self.callWhenRunning(self._reschedule) into startRunning instead of run, and then calling reactor.startRunning(). (In fact I believe this used to work at some point in the past.)

However, to be a supported API, a bit more is necessary; it should be easy to safely use such an API cooperatively so that multiple plugins may attach themselves to the same reactor without attaching two reactor instances to the same loop or raising exceptions because a reactor is already installed.

Each reactor foreign event-loop module will need some implementation of such an interface, but just to be clear about the scope of this ticket, it should be closed by a change where the interface is specified and at least one reactor implements it; we should file a ticket for the others.

Change History (2)

comment:1 Changed 22 months ago by exarkun

How about trying to implement this feature twice before calling the API a standard interface? Who is sufficiently familiar with two toolkits to actually design an interface that will work on them both without first implementing them both?

comment:2 Changed 22 months ago by noisecapella

This came about from a question I asked on StackOverflow regarding the ability to use Twisted within a Rhythmbox plugin, which is already running within the GTK event loop. I tried out Glyph's solution and it seems to work fine, although I haven't put it through any kind of rigorous testing. My updated plugin is here, on the gtkreactor branch.

Note: See TracTickets for help on using tickets.