Skip to content

Instantly share code, notes, and snippets.

@w495
Created October 30, 2013 16:47
Show Gist options
  • Save w495/7235979 to your computer and use it in GitHub Desktop.
Save w495/7235979 to your computer and use it in GitHub Desktop.
Try to test green threads in circuits. Fail!
from datetime import datetime
from circuits import Component, Event
class Computation(Event):
"""Computation Event"""
class Spawner(Component):
def __init__(self, *args, **kwargs):
super(Spawner, self).__init__()
self.bob = Spawn(*args, **kwargs).register(self)
class Spawn(Component):
def __init__(self, sname, function, *args, **kwargs):
super(Spawn, self).__init__()
self.sname = sname
self.function = function
self._result = None
self._args = args
self._kwargs = kwargs
print("I'm i %s", self.sname)
def started(self, *args):
print("I'm s b %s", self.sname)
self.fire(Computation())
print("I'm s e %s", self.sname)
def computation(self, *args):
print("I'm c b %s self._result = %s", self.sname, self._result)
self._result = self.function(*self._args, **self._kwargs)
print("I'm c e %s self._result = %s", self.sname, self._result)
#self.stop()
def result():
return self._result
def xxxpow():
print("xxxpow start")
time.sleep(4)
print("xxxpow end")
return 1
s1 = Spawner(1, xxxpow)
s2 = Spawner(2, xxxpow)
t1 = datetime.now()
(s1 + s2).run()
t2 = datetime.now()
d = t2 - t1
d.microseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment