run reactor in a loop (python twisted)

564 Views Asked by At

There is an external function (another module) that uses Twisted, so in order for it to work I need to call reactor.run() However I want to call that function multiple times. How can I do it? (I get ReactorNotRestartable error)

2

There are 2 best solutions below

0
On

If there's an API you want to call and the implementation of that API calls reactor.run then you cannot call it more than once.

Perhaps there is an alternate API you can call which does everything except for calling reactor.run. Then you can manage the reactor.run call in your own code (making sure to only call it once) while calling that alternate API as many times as you like.

0
On

You can run the twisted function in a background thread using [crochet][1], then bring the results into the foreground process when the execution finishes. Just don't execute reactor.stop() because that might mess things up. Also, crochet will take care of stopping the reactor.