def chat_server(addr): my_nick = until_eol().strip() while True: ev, val = first(until_eol=True, waits=['chat_message']) if ev == 'until_eol': fire('chat_message', (my_nick, val.strip())) else: nick, message = val send("<%s> %s\r\n" % (nick, message))
def chat_server(addr): my_nick = until_eol().strip() while True: evt, data = first(until_eol=True, waits=['chat_message']) if evt == 'until_eol': fire('chat_message', (my_nick, data.strip())) else: nick, message = data send("<%s> %s\r\n" % (nick, message))
def chat_server(addr): my_nick = until_eol().strip() while True: evt, data = first(until_eol=True, waits=["chat_message"]) if evt == "until_eol": fire("chat_message", (my_nick, data.strip())) else: nick, message = data send("<%s> %s\r\n" % (nick, message))
def f(): sleep(0.05) fire("boom!") sleep(0.05) WVPASS(v[0] == 2) sleep(0.05) fire("boom!") sleep(0.05) WVPASS(v[0] == 4) quickstop()
def query_20_times(): d = MongoClient(HOST, PORT) counts = [] wait('deleted') for i in range(20): with d.diesel.test.find({'type':'test'}, limit=500) as cursor: while not cursor.finished: counts.append(len(cursor.more())) if not i: wait('main.done') assert 0 in counts, counts assert 500 in counts, counts print "20 concurrent queries - passed" fire('queries.done', True)
def handle_transition(self, manual=False): '''Handle state change. ''' if not manual: diesel.fire(self) events = self.socket.getsockopt(zmq.EVENTS) if events & zmq.POLLIN: self.read_gate.set() else: self.read_gate.clear() if events & zmq.POLLOUT: self.write_gate.set() else: self.write_gate.clear()
def subq(self, classes): if type(classes) not in (set, list, tuple): classes = [classes] q = Queue() for cls in classes: self.sub_adds.append((cls, q)) fire(self.sub_wake_signal) try: yield q finally: for cls in classes: self.sub_rms.append((cls, q))
def publish_and_update(self, collection, payload): """Publish an update and yield None to relay the data to the backend.""" upsert, raw_bson = payload[:4], payload[4:] spec, raw_doc = _bson_to_dict(raw_bson) uid = self.channels[(collection, str(spec))].update(raw_doc) yield fire(('update', collection, str(spec))) yield up(None)
def chat_server(addr): my_nick = (yield until_eol()).strip() while True: my_message, other_message = yield (until_eol(), wait('chat_message')) if my_message: yield fire('chat_message', (my_nick, my_message.strip())) else: nick, message = other_message yield "<%s> %s\r\n" % (nick, message)
def getboard(): buf = '' yield sleep(0.1) while True: #print move_made #print "waiting for stdin ..." board, buf = tron.Board.read(buf) if not board: break yield fire('board-received', board) #print "fired board" yield wait('move-made')
def test_wait_tokens_dont_accumulate_forever(): """Wait tokens and related structures should be disposed of after use. They are tracked in a dictionary in the internal diesel.events.WaitPool. If a wait_id has no more objects waiting on it, it should be removed from that dictionary along with the set of waiting objects. """ done = [] wait_ids = [] expected_length = len(diesel.runtime.current_app.waits.waits) for i in xrange(50): wait_id = uuid.uuid4().hex diesel.fork(waiting_green_thread, wait_id, done) diesel.sleep() wait_ids.append(wait_id) for wait_id in wait_ids: diesel.fire(wait_id) diesel.sleep() while len(done) != 50: diesel.sleep(0.1) actual_length = len(diesel.runtime.current_app.waits.waits) assert actual_length == expected_length, actual_length
def messages_exist(self): '''Handle state change. ''' self.read_gate.set() diesel.fire(self)
def gunner(): x = 1 while True: fire('bam', x) x += 1 sleep()
def release(self): self.count += 1 fire(self.wait_id)
def f(): sleep(0.05) fire("fizz!") done.set()
def release(self): self.count += 1 fire(self)
def f(): sleep(0.05) fire("fizz!") sleep(0.05) WVPASS(v[0] == 0) # should not have woken up! quickstop()
def pure_db_action(): d = MongoClient(HOST, PORT) print d.list_databases() print d.drop_database('diesel') fire('deleted') d.diesel.test.insert({'name':'dowski', 'state':'OH'}) d.diesel.test.insert({'name':'jamwt', 'state':'CA'}) d.diesel.test.insert({'name':'mrshoe', 'state':'CA'}) with (d.diesel.test.find({'state':'OH'})) as cursor: while not cursor.finished: res = cursor.more() assert res[0]['name'] == 'dowski', res assert res[0]['state'] == 'OH', res print "query1 (simple where) passed" with d.diesel.test.find({'state':'CA'}) as cursor: while not cursor.finished: res = cursor.more() assert len(res) == 2, res assert res[0]['name'] == 'jamwt', res assert res[1]['name'] == 'mrshoe', res print "query2 (simple where) passed" with (d.diesel.test.find()) as cursor: while not cursor.finished: res = cursor.more() assert len(res) == 3, res assert [r['state'] for r in res] == ['OH', 'CA', 'CA'], res print "query3 (query all) passed" print "updating" d.diesel.test.update({'name':'dowski'}, {'$set':{'kids':2}}) with d.diesel.test.find() as cursor: while not cursor.finished: res = cursor.more() assert [r['kids'] for r in res if 'kids' in r] == [2], res print "query4 (verify update) passed" print "inserting" d.diesel.test.insert({'name':'mr t', 'state':'??'}) with d.diesel.test.find({'name':'mr t'}, ['state']) as cursor: while not cursor.finished: res = cursor.more() assert len(res) == 1, res assert 'name' not in res[0], res assert res[0]['state'] == '??', res print "query5 (verify insert) passed" print "deleting" d.diesel.test.delete({'name':'mr t'}) with d.diesel.test.find({'name':'mr t'}, ['state']) as cursor: while not cursor.finished: res = cursor.more() assert res == [], res print "query6 (verify delete) passed" print "inserting 10000" d.diesel.test.insert([{'code':i, 'type':'test'} for i in xrange(10000)]) count = 0 passes = 0 with d.diesel.test.find({'type':'test'}) as cursor: while not cursor.finished: count += len(cursor.more()) passes += 1 assert count == 10000, count assert passes == 2, passes print "query7 (get_more) passed" print "inserting" d.diesel.test.insert([{'letter':'m'}, {'letter':'b'}, {'letter':'k'}]) with d.diesel.test.find({'letter': {'$exists':True}}) as cursor: cursor.sort('letter', Ops.DESCENDING) while not cursor.finished: res = cursor.more() assert len(res) == 3, res assert [r['letter'] for r in res] == ['m', 'k', 'b'], res print "query8 (sorting) passed" with d.diesel.test.find({'type':'test'}) as cursor: n = cursor.count() assert n == 10000, n print "query9 (count) passed" n = 0 for rec in d.diesel.test.find({'type':'test'}): n += 1 assert n == 10000, n print "query10 (cursor iteration) passed" fire('main.done', True)
def work_with_group(name, group, message): print "Ho! Ho! Ho! Let's", message fire('%s-group-started' % name) sleep(random.random() * 3) excuse_group(name, group)
def set(self): if not self.is_set: self.is_set = True fire(self)
def pump(): for i in xrange(5): fire('thing') print "Fired 'thing'" sleep(1) quickstop()
def trigger(self): diesel.sleep() diesel.fire('ready') super(TestConsumersOnEmptyQueue, self).trigger()
def put(self, i=None): self.inp.append(i) fire(self)
def actor(): while True: board = yield wait('board-received') #print "got the board..." yield fire('move-made', tron.move(random_move(board)))
def __init__(self): for cls in classes: hb.sub_adds.append((cls, q)) fire(hb.sub_wake_signal)
def excuse_group(name, group): group[:] = [] fire('%s-group-done' % name, True)
def put(self, i=None): self.inp.append(i) fire(self.wait_id)
def f(): sleep(0.05) fire("boom!") sleep(0.05) fire("boom!") done.set()