back to Index

gevent_fieldcontrol

2014-07-19

field control with gevent


In [1]:
# Cross-notebook include shim
with open("nbinclude.ipynb") as nbinclude_f: # don't rename nbinclude_f
    import IPython.nbformat.current
    get_ipython().run_cell(IPython.nbformat.current.read(nbinclude_f, 'json').worksheets[0].cells[0].input)
nbinclude('netrobot')
nbinclude('simplekernel')
import time, lcm
from python.forseti2 import piemos_cmd
import IPython.display
import sys
import threading
import gevent

In [2]:
r=NetRobot()

In [3]:
r.cmd(1,1)

In [4]:
r.cmd(1,0)

In [5]:
r.cmd(0,1)

In [40]:
r.cmd(0,0)

In [6]:
r.cmd(0,.2)

In [7]:
def cmd_handler(channel, data):
    msg=piemos_cmd.decode(data)
    if msg.enabled:
        r.enable()
    else:
        r.disable()

In [8]:
def lcm_run():
    lc=lcm.LCM('udpm://239.255.76.67:7667?ttl=1')
    sub=lc.subscribe("piemos0/cmd", cmd_handler)
    while True:
        lc.handle()
        gevent.sleep(.01)

In [9]:
lcm_thread=gevent.spawn(lcm_run)

In [21]:
lcm_thread.start()

In [37]:
gevent.kill(lcm_thread)

In [39]:
gevent.joinall(lcm_thread)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-5bb16f48948c> in <module>()
----> 1 gevent.joinall(lcm_thread)

/usr/local/lib/python2.7/dist-packages/gevent/greenlet.pyc in joinall(greenlets, timeout, raise_error, count)
    398 def joinall(greenlets, timeout=None, raise_error=False, count=None):
    399     if not raise_error:
--> 400         wait(greenlets, timeout=timeout)
    401     else:
    402         for obj in iwait(greenlets, timeout=timeout):

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in wait(objects, timeout, count)
    643     result = []
    644     if count is None:
--> 645         return list(iwait(objects, timeout))
    646     for obj in iwait(objects=objects, timeout=timeout):
    647         result.append(obj)

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in iwait(objects, timeout)
    604         if timeout is not None:
    605             timer.stop()
--> 606         for obj in objects:
    607             unlink = getattr(obj, 'unlink', None)
    608             if unlink:

TypeError: 'Greenlet' object is not iterable

In [22]:
for i in range(100):
    gevent.sleep(1)


KeyboardInterrupt
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-22-ab708d0a3c60> in <module>()
      1 for i in range(100):
----> 2     gevent.sleep(1)

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in sleep(seconds, ref)
     73         waiter.get()
     74     else:
---> 75         hub.wait(loop.timer(seconds, ref=ref))
     76 
     77 

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in wait(self, watcher)
    339         watcher.start(waiter.switch, unique)
    340         try:
--> 341             result = waiter.get()
    342             assert result is unique, 'Invalid switch into %s: %r (expected %r)' % (getcurrent(), result, unique)
    343         finally:

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in get(self)
    566             self.greenlet = getcurrent()
    567             try:
--> 568                 return self.hub.switch()
    569             finally:
    570                 self.greenlet = None

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in switch(self)
    329         if switch_out is not None:
    330             switch_out()
--> 331         return greenlet.switch(self)
    332 
    333     def switch_out(self):

KeyboardInterrupt: 

In [20]:
lcm_thread.ready()


Out[20]:
True

In [65]:
lcm_thread.join()


KeyboardInterrupt
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-65-d24fd9d4201d> in <module>()
----> 1 lcm_thread.join()

/usr/local/lib/python2.7/dist-packages/gevent/greenlet.pyc in join(self, timeout)
    288                 t = Timeout.start_new(timeout)
    289                 try:
--> 290                     result = self.parent.switch()
    291                     assert result is self, 'Invalid switch into Greenlet.join(): %r' % (result, )
    292                 finally:

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in switch(self)
    329         if switch_out is not None:
    330             switch_out()
--> 331         return greenlet.switch(self)
    332 
    333     def switch_out(self):

KeyboardInterrupt: 

In [64]:
gevent.fork()


Out[64]:
12806

In [54]:
gevent.fork?

In [45]:
lcm_thread.fork()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-45-1ffa5a1690a4> in <module>()
----> 1 lcm_thread.fork()

AttributeError: 'Greenlet' object has no attribute 'fork'

In [40]:
lcm_thread.join()


Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run
    result = self._run(*self.args, **self.kwargs)
  File "<ipython-input-22-a264ddf146e0>", line 5, in lcm_run
    lc.handle()
KeyboardInterrupt
<Greenlet at 0x1751030: lcm_run> failed with KeyboardInterrupt

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-40-d24fd9d4201d> in <module>()
----> 1 lcm_thread.join()

/usr/local/lib/python2.7/dist-packages/gevent/greenlet.pyc in join(self, timeout)
    288                 t = Timeout.start_new(timeout)
    289                 try:
--> 290                     result = self.parent.switch()
    291                     assert result is self, 'Invalid switch into Greenlet.join(): %r' % (result, )
    292                 finally:

/usr/local/lib/python2.7/dist-packages/gevent/hub.pyc in switch(self)
    329         if switch_out is not None:
    330             switch_out()
--> 331         return greenlet.switch(self)
    332 
    333     def switch_out(self):

KeyboardInterrupt: 

In [29]:
lcm_thread.kill?

In [42]:
r.disable()

failed because gevent threads can't be forked: only run while gevent.sleep() or gevent.fork() is running in main process


In [ ]: