def server (port): s = shrike.tcp_socket() s.set_reuse_addr() s.bind (('', port)) s.listen (5) while not shutdown: conn, addr = s.accept() shrike.spawn (session, conn, addr)
W = sys.stderr.write z = 0 # two threads fight to increase or decrease a global integer n = 1000 def thing0 (c): global n while c: r = random.randint (0, 100) n += r c -= 1 shrike._yield() print 'exited thing0' def thing1 (c): global n while c: r = random.randint (0, 100) n -= r c -= 1 shrike._yield() print 'exited thing1' c0 = shrike.spawn (thing0, 100) c1 = shrike.spawn (thing1, 100) for i in range (101): shrike.run() print n
def session (conn, addr): while 1: block = conn.recv (1024) if not block: break elif block == 'quit\r\n': break elif block == 'shutdown\r\n': shrike._exit = True break else: conn.send (block) conn.close() def server (port): s = shrike.tcp_socket() s.set_reuse_addr() s.bind (('', port)) s.listen (5) while not shutdown: conn, addr = s.accept() shrike.spawn (session, conn, addr) # this doesn't quite cut it... need a proper _exit condition. shutdown = False shrike.spawn (server, 8888) shrike.event_loop()
import shrike import sys W = sys.stderr.write z = 0 def thing (x): global z while 1: W ('[%d]' % (z,)) z += x shrike._yield() return z c0 = shrike.spawn (thing, 5) print z for x in range (100): c0._resume (0) print z