Thread
s and Greenlet
s have different behavior in an interactive environment. The main event loop has to be hacked in some instances.
Greenlet
is from the gevent
module which is a concurrent task in python. It has an internal context switching separate from Python's (pthread) and the concurrency works really well (in my experience). Some of the issues with Greenlets is that they block on blocking C-system calls and socket interactions if they're not monkey-patched (module in gevent
).
The main event-loop needs to be patched in order for the greenlets to work correctly... If you spawn a greenlet in an interactive environment it will not switch contexts and execute, I forgot how off the top of my head how to patch the main event loop (will add later).
Example of failure:
In [1]: from gevent.greenlet import Greenlet
In [2]: def print_hi():
...: print 'hi'
...:
In [3]: print_hi()
hi
In [4]: g = Greenlet(print_hi)
In [5]: g.start()
Edit:
After looking at some of the code on this project here's how we hacked the ipython input hook to use gevent
import sys
import select
import gevent
def stdin_ready():
infds, outfds, erfds = select.select([sys.stdin], [], [], 0)
if infds:
return True
else:
return False
def inputhook_gevent():
try:
while not stdin_ready():
gevent.sleep(0.001)
except KeyboardInterrupt:
pass
return 0
# install the gevent inputhook
from IPython.lib.inputhook import inputhook_manager
inputhook_manager.set_inputhook(inputhook_gevent)
inputhook_manager._current_gui = 'gevent'
# First import the embeddable shell class
from IPython.frontend.terminal.embed import InteractiveShellEmbed
Patched Example:
In [6]: def say_hi():
...: print "hi"
...:
In [7]: g = gevent.greenlet.Greenlet(say_hi)
In [8]: g.start()
In [9]: hi <-- Cursor is here so it printed hi