On Dec 9, 2015, at 4:17 AM, Sinan Nalkaya <sardok@gmail.com> wrote:

Hi Everyone,

I am developing a tiny library where you can use your gpio pins (over sysfs) in twisted way. You may find more about, on https://github.com/sardok/txgpio.

I am facing with a race condition issue, where certain files are needed to be created in filesystem, by kernel, in order have the class to finish its initialization. Which means in twisted jargon, a deferred needs to be resolved in __init__ (or another method - see below) method in order to have complete object.

After reading this blog post http://as.ynchrono.us/2014/12/asynchronous-object-initialization.html by Jean-Paul, i believe having 'from_' factory method which waits for those certain files to be created before creating the actual class object is the better way.

However, i couldn't find a way to wait for a deferred in order to use the from_ function in the following form (which is the common form for using from_ factory methods);

obj = sysfs.GPIO.from_unexported_node(*a, **kw)

More or less, i am looking for some thing equivalent of 'asyncio.get_event_loop()' and 'yield from loop.run_in_executor()' as mentioned in the blog post.

The equivalent of 'asyncio.get_event_loop()' is simply 'from twisted.internet import reactor'.  However, relying upon this is an anti-pattern (I begged Guido not to add get_event_loop to asyncio, since we would love to remove 'twisted.internet.reactor' from Twisted, but oh well).  The "right" way to do this is simply to accept 'reactor' as a parameter to 'from_...'.

If you're using, for example, twisted.internet.task.react, you'll receive the 'reactor' as a parameter at the "top" of your program, you'll never have to import it from twisted.internet, and you can just pass it on through.  This style is much cleaner.

The equivalent to 'run_in_executor(None, ...)' is simply 'reactor.callInThread(...)'.

I'm glad you found the "right" way to initialize an object asynchronously :-).

Hope this helps,