def _shutdown_subsystems(self): # cleanup internals if self._timer is not None and self._timer.active(): self._timer.cancel() self._timer = None # shutdown middleware components dns_manager = DNSManager() account_manager = AccountManager() addressbook_manager = AddressbookManager() session_manager = SessionManager() procs = [ proc.spawn(dns_manager.stop), proc.spawn(account_manager.stop), proc.spawn(addressbook_manager.stop), proc.spawn(session_manager.stop) ] proc.waitall(procs) # stop video device self.video_device.producer.close() # shutdown engine self.engine.stop() self.engine.join(timeout=5) # stop threads thread_manager = ThreadManager() thread_manager.stop() # stop the reactor reactor.stop()
def _shutdown_subsystems(self): dns_manager = DNSManager() account_manager = AccountManager() session_manager = SessionManager() # terminate all sessions p = proc.spawn(session_manager.stop) p.wait() # shutdown SylkServer components procs = [proc.spawn(self.web_server.stop), proc.spawn(self.request_handler.stop), proc.spawn(self.thor_interface.stop)] proc.waitall(procs) # shutdown other middleware components procs = [proc.spawn(dns_manager.stop), proc.spawn(account_manager.stop)] proc.waitall(procs) # shutdown engine self.engine.stop() self.engine.join(timeout=5) # stop threads thread_manager = ThreadManager() thread_manager.stop() # stop the reactor reactor.stop()
def handler(local): client = str(local.getHost()) print 'accepted connection from %s' % client remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP( remote_host, remote_port) a = proc.spawn(forward, remote, local) b = proc.spawn(forward, local, remote) proc.waitall([a, b], trap_errors=True) print 'closed connection to %s' % client
def start(self): """ Start the accounts, which will determine the ones with the enabled flag set to activate. """ notification_center = NotificationCenter() notification_center.post_notification('SIPAccountManagerWillStart', sender=self) proc.waitall([proc.spawn(account.start) for account in self.accounts.itervalues()]) notification_center.post_notification('SIPAccountManagerDidStart', sender=self)
def stop(self): """ Stop the accounts, which will determine the ones that were enabled to deactivate. This method returns only once the accounts were stopped successfully or they timed out trying. """ notification_center = NotificationCenter() notification_center.post_notification('SIPAccountManagerWillEnd', sender=self) proc.waitall([proc.spawn(account.stop) for account in self.accounts.itervalues()]) notification_center.post_notification('SIPAccountManagerDidEnd', sender=self)
def start(self): """ Start the accounts, which will determine the ones with the enabled flag set to activate. """ notification_center = NotificationCenter() notification_center.post_notification('SIPAccountManagerWillStart', sender=self) proc.waitall( [proc.spawn(account.start) for account in self.accounts.values()]) notification_center.post_notification('SIPAccountManagerDidStart', sender=self)
def _deactivate(self): with self._activation_lock: if not self._active: return notification_center = NotificationCenter() notification_center.post_notification('SIPAccountWillDeactivate', sender=self) self._active = False handlers = [self._registrar, self._mwi_subscriber, self._pwi_subscriber, self._dwi_subscriber, self._presence_subscriber, self._self_presence_subscriber, self._dialog_subscriber, self._presence_publisher, self._dialog_publisher, self.xcap_manager] proc.waitall([proc.spawn(handler.stop) for handler in handlers]) notification_center.post_notification('SIPAccountDidDeactivate', sender=self)
def test_wait_all_exception_order(self): # if there're several exceptions raised, the earliest one must be raised by wait def badint(): sleep(0.1) int('first') a = proc.spawn(badint) b = proc.spawn(int, 'second') try: proc.waitall([a, b]) except ValueError, ex: assert 'second' in str(ex), repr(str(ex))
def stop(self): """ Stop the accounts, which will determine the ones that were enabled to deactivate. This method returns only once the accounts were stopped successfully or they timed out trying. """ notification_center = NotificationCenter() notification_center.post_notification('SIPAccountManagerWillEnd', sender=self) proc.waitall( [proc.spawn(account.stop) for account in self.accounts.values()]) notification_center.post_notification('SIPAccountManagerDidEnd', sender=self)
def test_wait_noerrors(self): x = proc.spawn(lambda: 1) y = proc.spawn(lambda: 2) z = proc.spawn(lambda: 3) self.assertEqual(proc.waitall([x, y, z]), [1, 2, 3]) e = coros.event() x.link(e) self.assertEqual(e.wait(), 1) x.unlink(e) e = coros.event() x.link(e) self.assertEqual(e.wait(), 1) self.assertEqual([proc.waitall([X]) for X in [x, y, z]], [[1], [2], [3]])
def test_write_chunk(self): client, server = proc.waitall(self.setup_two_endpoints()) self._test_write_chunk(client, server) self._test_write_chunk(server, client) #self.assertNoIncoming(0.1, client, server) client.loseConnection() server.loseConnection()
def test_close_connection__read(self): client, server = proc.waitall(self.setup_two_endpoints()) client.loseConnection() self.assertRaises(ConnectionDone, server.read_chunk) self.assertRaises(ConnectionDone, server.write, self.make_hello(server).encode()) self.assertRaises(ConnectionDone, client.read_chunk) self.assertRaises(ConnectionDone, client.write, self.make_hello(client).encode())
def test_deliver_chunk(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession(client), GreenMSRPSession(server) self._test_deliver_chunk(client, server) self._test_deliver_chunk(server, client) #self.assertNoIncoming(0.1, client, server) client.shutdown() server.shutdown()
def test_close_connection__receive(self): client, server = proc.waitall(self.setup_two_endpoints()) assert isinstance(client, MSRPTransport), repr(client) client, server = GreenMSRPSession(client), GreenMSRPSession(server) client.shutdown() self.assertRaises(ConnectionDone, server.receive_chunk) self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp)) self.assertRaises(ConnectionDone, client.receive_chunk) self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
def start(self): """ Start the accounts, which will determine the ones with the enabled flag set to activate. """ notification_center = NotificationCenter() notification_center.post_notification( 'SIPAccountManagerWillStart', sender=self, data=NotificationData(bonjour_available=_bonjour.available, accounts=list( a.id for a in self.accounts.values()))) proc.waitall([ proc.spawn(account.start) for account in list(self.accounts.values()) ]) notification_center.post_notification('SIPAccountManagerDidStart', sender=self)
def _deactivate(self): with self._activation_lock: if not self._active: return notification_center = NotificationCenter() notification_center.post_notification('SIPAccountWillDeactivate', sender=self) self._active = False handlers = [ self._registrar, self._mwi_subscriber, self._pwi_subscriber, self._dwi_subscriber, self._presence_subscriber, self._self_presence_subscriber, self._dialog_subscriber, self._presence_publisher, self._dialog_publisher, self.xcap_manager ] proc.waitall([proc.spawn(handler.stop) for handler in handlers]) notification_center.post_notification('SIPAccountDidDeactivate', sender=self)
def test_reader_failed__receive(self): # if reader fails with an exception, receive_chunk raises that exception # send_chunk raises an error and the other party gets closed connection client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession(client), GreenMSRPSession(server) client.reader_job.kill(InjectedError("Killing client's reader_job")) self.assertRaises(InjectedError, client.receive_chunk) self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp)) self.assertRaises(ConnectionClosed, server.receive_chunk) self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
def test_send_chunk_response_localtimeout(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession_ZeroTimeout(client), GreenMSRPSession(server) x = self.make_hello(client.msrp) self.assertRaisesCode(LocalResponse, 408, client.deliver_chunk, x) y = server.receive_chunk() self.assertSameData(x, y) #self.assertNoIncoming(0.1, client, server) server.shutdown() client.shutdown()
def test_reader_failed__send(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession(client), GreenMSRPSession(server) client.reader_job.kill(InjectedError("Killing client's reader_job")) api.sleep(0.1) self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp)) self.assertRaises(InjectedError, client.receive_chunk) api.sleep(0.1) self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp)) self.assertRaises(ConnectionClosed, server.receive_chunk)
def test_send_chunk_response_localtimeout(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession_ZeroTimeout( client), GreenMSRPSession(server) x = self.make_hello(client.msrp) self.assertRaisesCode(LocalResponse, 408, client.deliver_chunk, x) y = server.receive_chunk() self.assertSameData(x, y) #self.assertNoIncoming(0.1, client, server) server.shutdown() client.shutdown()
def test_deliver_chunk_success_report(self): client, server = proc.waitall(self.setup_two_endpoints()) client, server = GreenMSRPSession(client), GreenMSRPSession(server) chunk = self.make_hello(client.msrp, success_report='yes') self._test_deliver_chunk(client, server, chunk) report = client.receive_chunk() self.assertEqual(report.method, 'REPORT') self.assertEqual(report.message_id, chunk.message_id) self.assertEqual(report.byte_range, chunk.byte_range) client.shutdown() server.shutdown()
def test_wait_error(self): def x(): sleep(DELAY) return 1 x = proc.spawn(x) z = proc.spawn(lambda: 3) y = proc.spawn(int, 'badint') y.link(x) x.link(y) y.link(z) z.link(y) self.assertRaises(ValueError, proc.waitall, [x, y, z]) self.assertRaises(proc.LinkedFailed, proc.waitall, [x]) self.assertEqual(proc.waitall([z]), [3]) self.assertRaises(ValueError, proc.waitall, [y])
def waitall(self): return proc.waitall(self.greenlets)
from eventlib.green import socket # this example works with both standard eventlib hubs and with twisted-based hub # uncomment the following line to use twisted hub #from twisted.internet import reactor def geturl(url): c = socket.socket() ip = socket.gethostbyname(url) c.connect((ip, 80)) print '%s connected' % url c.send('GET /\r\n\r\n') return c.recv(1024) urls = [ 'www.google.com', 'www.yandex.ru', 'www.python.org', 'ag-projects.com', 'sylkserver.com' ] jobs = [proc.spawn(geturl, x) for x in urls] print 'spawned %s jobs' % len(jobs) # collect the results from workers results = proc.waitall(jobs) # Note, that any exception in the workers will be reraised by waitall # unless trap_errors argument specifies otherwise for url, result in zip(urls, results): print '%s: %s' % (url, repr(result)[:50])