back to Index
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)
In [22]:
for i in range(100):
gevent.sleep(1)
In [20]:
lcm_thread.ready()
Out[20]:
In [65]:
lcm_thread.join()
In [64]:
gevent.fork()
Out[64]:
In [54]:
gevent.fork?
In [45]:
lcm_thread.fork()
In [40]:
lcm_thread.join()
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 [ ]: