def main(): disp = Dispatcher() Tasklet.new(disp.loop)() host = Host(disp, "/dev/ttySP1") disp.add_host(host) while True: host.send([0x45])
def client(): try: remote_client = RemoteClient() remote_client.connect(('localhost', 9081)) remote_task = remote_client.lookup('testing123') self.assertFalse(remote_task is None) MSG_TEST.send(remote_task)(20, 30) MSG_TEST.send(remote_task)(30, 40) client_results.append(MSG_SUM.call(remote_task)(10, 20, 30)) client_results.append(MSG_SUM.call(remote_task)(10, 20, 30)) MSG_TEST.send(remote_task)(20, 30) MSG_TEST.send(remote_task)(30, 40) MSG_SLEEP.call(remote_task)(1) try: MSG_SLEEP.call(remote_task, timeout = 1)(2) self.fail("expected timeout") except TimeoutError: pass #expected MSG_QUIT.send(remote_task)() Tasklet.sleep(2) remote_client.close() except Exception: logging.exception("") self.fail("")
def accept_iter(self): while True: try: yield self.accept() except Exception: self.log.exception("in accept_iter") Tasklet.sleep(1.0) #prevent hogging
def callback(socket, count, event, args, kwargs): print count, event, Tasklet.current() if (count, event) == (1, "write"): pass elif (count, event) == (2, "read"): Tasklet.sleep(1.0) return "OK\r\n"
def __receive(self): # Called in the receiving tasklet reader = self.__stream.reader try: buffer = "" processData = True while True: line = reader.read_line() if line <> '---': buffer += line + "\n" else: try: data = yaml.load(buffer) processData = True if data == 'ping': processData = False self.sendData('pong') except yaml.parser.ParserError: q.logger.log("[SocketTaskHandler] Received bad formatted data: " + str(buffer), 3) else: if processData: q.logger.log("[SocketTaskHandler] Received data: " + str(data), 5) Tasklet.new(self.__messageHandler)(data, self) buffer = "" except EOFError: q.logger.log("[SocketTaskHandler] Client disconnected.", 3) MSG_SOCKET_CLOSE.send(self.__sending_tasklet)() self.__stream.close()
def testJoinAll(self): def sub0(): raise Exception("a proper exc") def sub1(): return 1 def sub2(): return 2 def sub3(): raise Exception("test exc") subs = [Tasklet.new(sub)() for sub in [sub0, sub1, sub2, sub3]] results = Tasklet.join_all(subs) self.assertTrue(isinstance(results[0], JoinError)) self.assertTrue(isinstance(results[0].cause, Exception)) self.assertEquals("a proper exc", str(results[0].cause), Exception) self.assertEquals(1, results[1]) self.assertEquals(2, results[2]) self.assertTrue(isinstance(results[3], JoinError)) self.assertTrue(isinstance(results[3].cause, Exception)) self.assertEquals("test exc", str(results[3].cause), Exception)
def server(): server_endpoint = None try: remote_server = RemoteServer() server_endpoint = remote_server.serve(('localhost', 9081)) remote_server.register('testing123') for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_SUM): server_results.append('s') msg.reply(sum(args)) elif msg.match(MSG_TEST): server_results.append('t') elif msg.match(MSG_QUIT): server_results.append('q') break elif msg.match(MSG_SLEEP): server_results.append('sl') Tasklet.sleep(args[0]) msg.reply(True) except Exception: logging.exception("") self.fail("") finally: if server_endpoint is not None: server_endpoint.close()
def a(): while True: #for i in xrange(0, 3): # try: # print "Line %d ReadROM: %s" % (i + 1, dispatcher.request(MicroLANReadROM(i))) # except MicroLANError as e: # print "Line %d ReadROM: %s" % (i + 1, type(e).__name__) devs = {} for i in xrange(0, 3): try: req = MicroLANListAll(dispatcher, i) devices = req.execute() except MicroLANError as e: print "Line %d SearchROM: %s" % (i + 1, type(e).__name__) else: #print "Line %d SearchROM: %s" % (i + 1, devices) if devices: try: dispatcher.request(MicroLANConvertTemperature(i)) except MicroLANError as e: print "Line %d ConvertTemperature: %s" % (i + 1, type(e).__name__) else: devs[i] = devices Tasklet.sleep(1) print "---" for i in xrange(0, 3): devices = devs.get(i) if devices: print "Line %d" % (i + 1) for dev in devices: try: print "ReadTemperature of %s: %.1f" % (dev, dispatcher.request(MicroLANReadTemperature(i, dev))) except MicroLANError as e: print "ReadTemperature of %s: %s" % (dev, type(e).__name__) Tasklet.sleep(2)
def connect(self, addr): assert self._stream is None, "must not be disconneted before connecting" self._stream = BufferedStream(Socket.connect(addr, Timeout.current())) self._command_queue = Deque() self._response_queue = Deque() self._command_writer_task = Tasklet.new(self._command_writer)() self._response_reader_task = Tasklet.new(self._response_reader)()
def sendToDrp(self, name, *args, **kwargs): MSG_DRP_CALL.send(self.__tasklet)(Tasklet.current(), name, *args, **kwargs) (msg, args, kwargs) = Tasklet.receive().next() if msg.match(MSG_DRP_RETURN): return args[0] elif msg.match(MSG_DRP_EXCEPTION): raise args[0]
def testDeadlocks(self): def process(cnn, cur, val): try: cur.execute("begin") cur.execute("insert into tbltest (test_id) values (1)") cur.execute("select sleep(2)") cur.execute("update tbltest set test_id=%d" % val) cur.execute("select sleep(2)") cur.execute("commit") return False except dbapi.Error as e: return "deadlock" in str(e).lower() cnn1 = dbapi.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) cur1 = cnn1.cursor() cnn2 = dbapi.connect(host = DB_HOST, user = DB_USER, passwd = DB_PASSWD, db = DB_DB) cur2 = cnn2.cursor() t1 = Tasklet.new(process)(cnn1, cur1, 2) t2 = Tasklet.new(process)(cnn2, cur2, 3) res = Tasklet.join_all([t1, t2]) self.assertTrue(res[0] or res[1], 'At least one of the queries expected to fail due to deadlock (innodb must be used)') # Both connections must survive after error cur1.execute("select 1") cur2.execute("select 2") cur1.close() cnn1.close() cur2.close() cnn2.close()
def testMultiClientMultiServer(self): N = 40 * 100 keys = ['test%d' % i for i in range(N)] mc = Memcache() mc.set_servers([ ((MEMCACHE_IP, 11211), 100), ((MEMCACHE_IP, 11212), 100), ((MEMCACHE_IP, 11213), 100), ((MEMCACHE_IP, 11214), 100) ]) with unittest.timer() as tmr: for i in range(N): self.assertEquals(MemcacheResult.STORED, mc.set(keys[i], 'hello world %d' % i)) print 'single client multi server single set keys/sec', tmr.sec(N) stride = 40 def fetcher(): for i in range(0, N, stride): result, values = mc.get_multi(keys[i:i + stride]) self.assertEquals(MemcacheResult.OK, result) self.assertEquals(stride, len(values)) for nr_clients in [2, 4, 8, 16]: #,32,64,128]: with unittest.timer() as tmr: for i in range(nr_clients): Tasklet.new(fetcher)() Tasklet.join_children() print 'multi client (%d), multi server multi get (%d) keys/sec' % ( nr_clients, stride), tmr.sec(N * nr_clients)
def testPushPop(self): self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Timeout.push(30) self.assertAlmostEqual(30, Timeout.current(), places=1) Timeout.pop() self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Timeout.push(30) self.assertAlmostEqual(30, Timeout.current(), places=1) Tasklet.sleep(1.0) self.assertAlmostEqual(29, Timeout.current(), places=1) #push a temporary short timeout Timeout.push(5) self.assertAlmostEqual(5, Timeout.current(), places=1) Timeout.pop() self.assertAlmostEqual(29, Timeout.current(), places=1) #try to push a new longer timeout than the parent timeout #this should fail, e.g. it will keep the parent timeout Timeout.push(60) self.assertAlmostEqual(29, Timeout.current(), places=1) Timeout.pop() self.assertAlmostEqual(29, Timeout.current(), places=1) Timeout.pop() self.assertEquals(TIMEOUT_NEVER, Timeout.current())
def testPushPop(self): self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Timeout.push(30) self.assertAlmostEqual(30, Timeout.current(), places = 1) Timeout.pop() self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Timeout.push(30) self.assertAlmostEqual(30, Timeout.current(), places = 1) Tasklet.sleep(1.0) self.assertAlmostEqual(29, Timeout.current(), places = 1) #push a temporary short timeout Timeout.push(5) self.assertAlmostEqual(5, Timeout.current(), places = 1) Timeout.pop() self.assertAlmostEqual(29, Timeout.current(), places = 1) #try to push a new longer timeout than the parent timeout #this should fail, e.g. it will keep the parent timeout Timeout.push(60) self.assertAlmostEqual(29, Timeout.current(), places = 1) Timeout.pop() self.assertAlmostEqual(29, Timeout.current(), places = 1) Timeout.pop() self.assertEquals(TIMEOUT_NEVER, Timeout.current())
def testLock(self): lock = Lock() self.assertEquals(True, lock.acquire()) self.assertEquals(True, lock.is_locked()) self.assertEquals(None, lock.release()) xs = [] def t(x): try: with lock: Tasklet.sleep(1.0) xs.append(x) return x except TimeoutError: pass start = time.time() for i in range(5): Tasklet.new(t)(i) join_result = Tasklet.join_children() self.assertEquals(5, len(join_result)) self.assertEquals(10, sum(xs)) end = time.time() self.assertAlmostEqual(5.0, end - start, places = 1)
def testMessageSend(self): class MSG_PONG(Message): pass class MSG_PING(Message): pass def c(parent): for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_PING): self.assertEquals((10, ), args) MSG_PONG.send(parent)(20) parent = Tasklet.current() child = Tasklet.new(c)(parent) i = 0 MSG_PING.send(child)(10) for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_PONG): self.assertEquals((20, ), args) i += 1 if i > 5: break MSG_PING.send(child)(10) self.assertEquals(6, i) try: start = time.time() for msg, args, kwargs in Tasklet.receive(2.0): self.fail('expected timeout error') except TimeoutError: end = time.time() self.assertAlmostEqual(2.0, end - start, places = 1) child.kill()
def __init__(self, connector, dbargs, max_connections=10, connect_timeout=-1, max_connection_age=None, max_connection_age_reaper_interval=60): super(Pool, self).__init__(connector, dbargs, connect_timeout) self._max_connections = max_connections self._max_connection_age = max_connection_age #some statistics self._queue_wait_timer_statistic = StatisticExtra() self._queue_wait_tasks_statistic = StatisticExtra() self._pool = Deque() #the pool of available idle connections #watch for server disconnects on idle connections: self._idle_disconnect_channel = Channel() self._idle_disconnect_reaper_task = Tasklet.loop( self._idle_disconnect_reaper, daemon=True)() #check for old connections if self._max_connection_age is not None: self._old_connection_reaper_task = Tasklet.interval( max_connection_age_reaper_interval, self._old_connection_reaper, daemon=True)()
def testMessageSend(self): class MSG_PONG(Message): pass class MSG_PING(Message): pass def c(parent): for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_PING): self.assertEquals((10, ), args) MSG_PONG.send(parent)(20) parent = Tasklet.current() child = Tasklet.new(c)(parent) i = 0 MSG_PING.send(child)(10) for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_PONG): self.assertEquals((20, ), args) i += 1 if i > 5: break MSG_PING.send(child)(10) self.assertEquals(6, i) try: start = time.time() for msg, args, kwargs in Tasklet.receive(2.0): self.fail('expected timeout error') except TimeoutError: end = time.time() self.assertAlmostEqual(2.0, end - start, places=1) child.kill()
def testMaxAge(self): pool = Pool(client, DB_ARGS, max_connections=2, connect_timeout=2, max_connection_age=2, max_connection_age_reaper_interval=1) new, cnn1 = pool.connect() new, cnn2 = pool.connect() pool.disconnect(cnn1) self.assertTrue(cnn1.is_connected()) self.assertTrue(cnn2.is_connected()) Tasklet.sleep(3) #cnn1 was idle, should be disconnected now by old age reaper self.assertFalse(cnn1.is_connected()) self.assertTrue(cnn2.is_connected()) pool.disconnect(cnn2) #cnn2 should be closed on old age self.assertFalse(cnn1.is_connected()) self.assertFalse(cnn2.is_connected())
def handle_accept(self): if self.acceptChannel and self.acceptChannel.balance < 0: t = asyncore.dispatcher.accept(self) if t is None: return t[0].setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) Tasklet.new(self.acceptChannel.send)(t)
def query_services(self, service_type, url, timeout=10, call_int=False, delay=0, *args, **kwargs): inst = self.app().inst daemons = inst.int_app.obj(DBCluster, "daemons", silent=True) tasklets = [] for dmnid, dmninfo in daemons.data.items(): services = dmninfo.get("services", {}) for svcid, svcinfo in services.items(): if svcinfo.get("type") != service_type: continue if call_int: found = False for sid, sinfo in services.items(): if sinfo.get("type") != "int": continue found = True svcid = sid svcinfo = sinfo break if not found: continue if "addr" not in svcinfo: continue if "port" not in svcinfo: continue task = Tasklet.new(self.do_query_service_exc) tasklets.append(task) task(svcid, svcinfo, url, timeout, *args, **kwargs) if delay > 0: Tasklet.sleep(delay) Tasklet.join_all(tasklets)
def main(): try: dispatcher = Dispatcher() host = Host(dispatcher, "/dev/ttyUSB0") dispatcher.add_host(host) Tasklet.new(dispatcher.loop)() print "Calibration: %s" % host.calibrate_baudrate() print "Available: %s" % host.available() dev = RadioDevice(dispatcher, 2) print "Protocol version: %s" % dev.protocol_version # Running HTTP server application = django.core.handlers.wsgi.WSGIHandler() def app_wrapper(*args, **kwargs): try: return application(*args, **kwargs) except Exception as e: print e server = WSGIServer(application) server.serve(('localhost', 8080)) while True: print "ADC data: %s, supply voltage: %.2f" % (dev.adc_data(), dev.supply_voltage()) Tasklet.sleep(1) os._exit(0) except Exception as e: logging.exception(e) os._exit(1)
def main(): options = parse_options() options.dbargs = {'host': options.host, 'port': options.port, 'user': options.user, 'passwd': options.passwd, 'db': options.db} if options.use_pool: options.pool = Pool(client, options.dbargs, options.use_pool) for i in range(options.sessions): session = Session(options) Tasklet.new(session.run)() try: query_count = options.query_count start = time.time() query_count_done = sum(Tasklet.join_children()) end = time.time() print end - start, query_count_done, query_count_done / (end - start) except Exception: logging.exception("must be an error in one of the sessions") quit()
def testMultiClientMultiServer(self): N = 40 * 100 keys = ['test%d' % i for i in range(N)] mc = Memcache() mc.set_servers([((MEMCACHE_IP, 11212), 100), ((MEMCACHE_IP, 11213), 100), ((MEMCACHE_IP, 11214), 100), ((MEMCACHE_IP, 11215), 100)]) with unittest.timer() as tmr: for i in range(N): self.assertEquals(MemcacheResult.STORED, mc.set(keys[i], 'hello world %d' % i)) print 'single client multi server single set keys/sec', tmr.sec(N) stride = 40 def fetcher(): for i in range(0, N, stride): result, values = mc.get_multi(keys[i:i+stride]) self.assertEquals(MemcacheResult.OK, result) self.assertEquals(stride, len(values)) for nr_clients in [2,4,8,16]:#,32,64,128]: with unittest.timer() as tmr: for i in range(nr_clients): Tasklet.new(fetcher)() Tasklet.join_children() print 'multi client (%d), multi server multi get (%d) keys/sec' % (nr_clients, stride), tmr.sec(N * nr_clients)
def testerrors(self): mc = Memcached(pool=MemcachedPool(size=4)) tasks = [] for i in xrange(0, 100): tasks.append(Tasklet.new(self.error_thread)(mc)) for i in xrange(0, 100): tasks.append(Tasklet.new(self.handled_thread)(mc)) Tasklet.join_all(tasks)
def handle_connect_event(self): err = self.socket.getsockopt(SOL_SOCKET, SO_ERROR) if err != 0: self.connected = False Tasklet.new(self.recvChannel.send_exception)(stdsocket.error, err) else: self.handle_connect() self.connected = True
def tearDown(self): MemcacheConnectionManager.create("default").close_all() cmd = 'killall %s' % MEMCACHED_BIN self.log.debug(cmd) os.system(cmd) Tasklet.sleep(1.0) #should be enough for memcached to go down
def log(self, message): if hasattr(Tasklet.current(), 'jobguid'): jobguid = Tasklet.current().jobguid q.workflowengine.jobmanager.appendJobLog(jobguid, message) else: # No job in the context, do nothing pass return True
def ping(self): while True: if self._calibrated: pkt = [0xf2] for i in xrange(0, 8): pkt.append(random.randint(0, 255)) self.send(pkt) Tasklet.sleep(7)
def t(x): try: with sema: Tasklet.sleep(1.0) xs.append(x) return x except TimeoutError: pass
def testBlock(self): d = Deque() Tasklet.later(1.0, d.append)(20) s = time.time() #should block on pop self.assertEquals(20, d.pop(True)) e = time.time() self.assertTrue((e - s) > 1.0)
def sendall(self, data, flags=0): # WARNING: this will busy wait until all data is sent # It should be possible to do away with the busy wait with # the use of a channel. self.sendBuffer += data while self.sendBuffer: Tasklet.sleep(0.01) return len(data)
def __init__(self, dispatcher, devname): "Constructor. devname - serial port devname" self.dispatcher = dispatcher self.stream = SerialStream(devname) self._next_pkt_id = 0 self._devname = devname self._sendlock = Lock() self._calibrated = False Tasklet.new(self.auto_calibrate_baudrate)()
def c(): for msg, args, kwargs in Tasklet.receive(): if msg.match(MSG_TEST_SUM): msg.reply(sum(args)) elif msg.match(MSG_TEST_MAX): msg.reply(max(args)) elif msg.match(MSG_TEST_SLEEP): Tasklet.sleep(args[0]) msg.reply(True)
def testPushPop4(self): self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Tasklet.set_current_timeout(10.0) self.assertAlmostEqual(10.0, Timeout.current(), places = 1) Timeout.push(TIMEOUT_CURRENT) self.assertAlmostEqual(10.0, Timeout.current(), places = 1) Timeout.pop() self.assertAlmostEqual(10.0, Timeout.current(), places = 1) Tasklet.set_current_timeout(TIMEOUT_NEVER)
def main(timeout = None): logging.basicConfig() logging.root.setLevel(logging.DEBUG) if timeout is not None: Tasklet.later(timeout, quit, name = 'unittest_timeout')(EXIT_CODE_TIMEOUT) dispatch(unittest.main)
def memcachedRun(self): self.log.debug("using memcached daemon: %s", MEMCACHED_BIN) for i in range(4): cmd = '%s -m 10 -p %d -u nobody -l 127.0.0.1&' % (MEMCACHED_BIN, 11212 + i) self.log.debug(cmd) os.system(cmd) Tasklet.sleep(1.0) #should be enough for memcached to get up and running
def testPushPop4(self): self.assertEquals(TIMEOUT_NEVER, Timeout.current()) Tasklet.set_current_timeout(10.0) self.assertAlmostEqual(10.0, Timeout.current(), places=1) Timeout.push(TIMEOUT_CURRENT) self.assertAlmostEqual(10.0, Timeout.current(), places=1) Timeout.pop() self.assertAlmostEqual(10.0, Timeout.current(), places=1) Tasklet.set_current_timeout(TIMEOUT_NEVER)
def testJoinResult(self): """test normal join and checks that parent will get child result""" def child(i): return i ch1 = Tasklet.new(child)(1) ch2 = Tasklet.new(child)(2) self.assertEquals(1, Tasklet.join(ch1)) self.assertEquals(2, Tasklet.join(ch2))
def ManageSockets(): global managerRunning while len(asyncore.socket_map): # Check the sockets for activity. asyncore.poll(0.05) # Yield to give other tasklets a chance to be scheduled. Tasklet.sleep(0.01) managerRunning = False
def server(): """accepts connections on a socket, and dispatches new tasks for handling the incoming requests""" server_socket = Socket.new() server_socket.bind(('localhost', 1337)) server_socket.listen() while True: client_socket = server_socket.accept() Tasklet.new(handler)(client_socket)