crochet
crochet copied to clipboard
Some way of enforcing that a function is called in the reactor
@crochet.run_in_reactor lets you easily call into the reactor from another thread. You may, however, have a method that is meant to be async twisted code. It would be nice if you could decorate it in such a way that it raises an exception if you call it in another thread. e.g.
@crochet.run_in_reactor
def meth_to_call_from_threads(self)
d = self.async_code()
d.addCallback(....)
@crochet.ensure_in_reactor
def async_code(self)
# Please raise an error if I am called from outside the reactor.
....
This would catch a case where, e.g. you forget to add the @run_in_reactor decorator to the first method.
That seems like it might be useful. Care to submit a patch? The changes I just made to @run_in_reactor could work as a basis for this. Otherwise I'll get around to it.
I should note that in general I feel Twisted code should be private, so something like:
def public_api():
return _private_api().wait()
@run_in_reactor
def _private_api():
return someTwistedStuff()
Sorry about the long delay. I may look into making a patch if I start working on a project that would need the feature, which could be soon :)