nlloyd / SubliminalCollaborator

Sublime Text 2 Plugin for remote pair programming
Apache License 2.0
213 stars 8 forks source link

allow other sublime plugins to use twisted library safely #24

Closed nlloyd closed 10 years ago

nlloyd commented 11 years ago

Following recommendations in issue #23. Not quite sure how to handle this, however it is a separate issue from that of #23 so wanted to track is separately.

phisch commented 11 years ago

please contact me if you have found a nice solution for this, i currently need the twisted library for a plugin too

nlloyd commented 11 years ago

An idea. The Twisted reactor (all implementations) have a "running" property that could be leveraged instead of my current globals mess.

Here is my proposal: Assuming that __threadedselect_ is the correct reactor to use, for each of our plugins we post in the documentation the following code to implement in the main plugin file at the top to safely share the twisted engine:

# install the ThreadedSelect reactor if no reactor is installed
if not 'twisted.internet.reactor' in sys.modules:
    from twisted.internet import _threadedselect
    _threadedselect.install()

def callInSublimeLoop(funcToCall):
    sublime.set_timeout(funcToCall, 0)

# start the reactor if it isn't running already, assuming this is the correct reactor...
from twisted.internet import reactor
if not reactor.running:
    reactor.interleave(callInSublimeLoop, installSignalHandlers=False)

I do not have this fully tested yet but looking at the Twisted library this would appear to be safe and sane. To my eyes this appears to be generic enough to start a Sublime-friendly Twisted reactor for anyone to use.

Thoughts?