crochet icon indicating copy to clipboard operation
crochet copied to clipboard

Some way of enforcing that a function is called in the reactor

Open brickZA opened this issue 12 years ago • 2 comments

@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.

brickZA avatar Sep 04 '13 14:09 brickZA

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()

itamarst avatar Sep 05 '13 20:09 itamarst

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 :)

brickZA avatar Feb 27 '14 08:02 brickZA