Skip to content

Instantly share code, notes, and snippets.

@prologic
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prologic/f95e2fe451b5a8eccfa9 to your computer and use it in GitHub Desktop.
Save prologic/f95e2fe451b5a8eccfa9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
from circuits import Component, Debugger, Event
class condition(Event):
"""condition Event"""
class waiter(Event):
"""waiter Event"""
class notifier(Event):
"""notifier Event"""
class App(Component):
def started(self, *args):
self.fire(waiter())
yield
self.fire(notifier())
yield
raise SystemExit(0)
def waiter(self):
print("I'll wait right here")
yield self.wait("condition")
print("I'm done waiting")
def notifier(self):
print("About to notify")
self.fire(condition())
print("Done notifying")
def generate_events(self, event):
print(".")
event.reduce_time_left(0.1)
(App() + Debugger()).run()
#!/usr/bin/env python
from __future__ import print_function
from circuits import Component, Debugger, Event
class producer(Event):
"""producer Event"""
class work(Event):
"""work Event"""
class Consumer(Component):
def work(self, i):
print("Doing work on {0:d}".format(i))
# XXX: We don't have an asynchronous sleep() :)
class App(Component):
def started(self, *args):
Consumer().register(self)
self.fire(producer())
yield
raise SystemExit(0)
def producer(self):
for i in range(4):
self.fire(work(i))
print("Put {0:d}".format(i))
(App() + Debugger()).run()
@prologic
Copy link
Author

Demo:

#!bash
$ ./test_condition.py 
<registered[*] (<Debugger/* 17139:MainThread (queued=0) [S]>, <App/* 17139:MainThread (queued=2) [R]> )>
<started[*] (<App/* 17139:MainThread (queued=1) [R]> )>
.
<waiter[*] ( )>
.
I'll wait right here
<notifier[*] ( )>
About to notify
Done notifying
.
<condition[*] ( )>
<stopped[*] (<App/* 17139:MainThread (queued=1) [S]> )>
<condition_done[*] (None )>
I'm done waiting

@prologic
Copy link
Author

Updated

@prologic
Copy link
Author

Demo of test_queue:

#!bash
$ ./test_queue.py 
<registered[*] (<Debugger/* 19046:MainThread (queued=0) [S]>, <App/* 19046:MainThread (queued=2) [R]> )>
<started[*] (<App/* 19046:MainThread (queued=1) [R]> )>
<registered[*] (<Consumer/* 19046:MainThread (queued=0) [S]>, <App/* 19046:MainThread (queued=2) [R]> )>
<producer[*] ( )>
Put 0
Put 1
Put 2
Put 3
<work[*] (0 )>
Doing work on 0
<work[*] (1 )>
Doing work on 1
<work[*] (2 )>
Doing work on 2
<work[*] (3 )>
Doing work on 3
<stopped[*] (<App/* 19046:MainThread (queued=0) [S]> )>

@prologic
Copy link
Author

And just for good measure; we're just implemented [async sleep] for circuits: circuits/circuits#62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment