Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
brickZA opened this issue Sep 4, 2013 · 2 comments
Open

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

brickZA opened this issue Sep 4, 2013 · 2 comments

Comments

@brickZA
Copy link

brickZA commented Sep 4, 2013

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

@itamarst
Copy link
Owner

itamarst commented Sep 5, 2013

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

@brickZA
Copy link
Author

brickZA commented Feb 27, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants