def run(loop, fut): thread_loop = tulip.new_event_loop() tulip.set_event_loop(thread_loop) server = thread_loop.run_until_complete( thread_loop.create_server( lambda: TestHttpServer(keep_alive=0.5), host, port, ssl=sslcontext)) waiter = tulip.Future(loop=thread_loop) loop.call_soon_threadsafe( fut.set_result, (thread_loop, waiter, server.sockets[0].getsockname())) try: thread_loop.run_until_complete(waiter) finally: # call pending connection_made if present run_briefly(thread_loop) # close opened trnsports for tr in transports: tr.close() run_briefly(thread_loop) # call close callbacks server.close() thread_loop.stop() thread_loop.close() gc.collect()
def start(self): assert not self._started self._started = True up_read, up_write = os.pipe() down_read, down_write = os.pipe() args, sock = self.args, self.sock pid = os.fork() if pid: # parent os.close(up_read) os.close(down_write) tulip.async(self.connect(pid, up_write, down_read)) else: # child os.close(up_write) os.close(down_read) # cleanup after fork tulip.set_event_loop(None) # setup process process = ChildProcess(up_read, down_write, args, sock) process.start()
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(self.loop) self.transport = unittest.mock.Mock() self.stream = tulip.StreamBuffer() self.response = HttpResponse('get', 'http://python.org')
def run(loop, fut): thread_loop = tulip.new_event_loop() tulip.set_event_loop(thread_loop) socks = thread_loop.run_until_complete( thread_loop.start_serving( lambda: TestHttpServer(keep_alive=0.5), host, port, ssl=sslcontext)) waiter = tulip.Future() loop.call_soon_threadsafe( fut.set_result, (thread_loop, waiter, socks[0].getsockname())) thread_loop.run_until_complete(waiter) # close opened trnsports for tr in transports: tr.close() run_once(thread_loop) # call close callbacks for s in socks: thread_loop.stop_serving(s) thread_loop.stop() thread_loop.close() gc.collect()
def __init__(self): self.loop = tulip.get_event_loop() args = ARGS.parse_args() if ':' in args.host: args.host, port = args.host.split(':', 1) args.port = int(port) if args.iocp: from tulip import windows_events sys.argv.remove('--iocp') logging.info('using iocp') el = windows_events.ProactorEventLoop() tulip.set_event_loop(el) if args.ssl: here = os.path.join(os.path.dirname(__file__), 'tests') if args.certfile: certfile = args.certfile or os.path.join(here, 'sample.crt') keyfile = args.keyfile or os.path.join(here, 'sample.key') else: certfile = os.path.join(here, 'sample.crt') keyfile = os.path.join(here, 'sample.key') sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) sslcontext.load_cert_chain(certfile, keyfile) else: sslcontext = None self.ssl = sslcontext self.args = args self.workers = []
def run(loop, fut): thread_loop = tulip.new_event_loop() tulip.set_event_loop(thread_loop) socks = thread_loop.run_until_complete( thread_loop.start_serving(lambda: TestHttpServer(keep_alive=0.5), host, port, ssl=sslcontext)) waiter = tulip.Future(loop=thread_loop) loop.call_soon_threadsafe( fut.set_result, (thread_loop, waiter, socks[0].getsockname())) thread_loop.run_until_complete(waiter) # close opened trnsports for tr in transports: tr.close() run_briefly(thread_loop) # call close callbacks for s in socks: thread_loop.stop_serving(s) thread_loop.stop() thread_loop.close() gc.collect()
def start(self): assert not self._started self._started = True up_read, up_write = os.pipe() down_read, down_write = os.pipe() args, sock = self.args, self.sock pid = os.fork() if pid: # parent os.close(up_read) os.close(down_write) self.connect(pid, up_write, down_read) else: # child os.close(up_write) os.close(down_read) # cleanup after fork tulip.set_event_loop(None) # setup process process = ChildProcess(up_read, down_write, args, sock, self.protocol_factory, self.ssl) process.start()
def init_process(self): # create new event_loop after fork tulip.get_event_loop().close() self.ev_loop = tulip.new_event_loop() tulip.set_event_loop(self.ev_loop) super().init_process()
def _io_event_loop_thread(self): """Worker thread for running the I/O event loop""" io_event_loop = tulip.get_event_loop_policy().new_event_loop() tulip.set_event_loop(io_event_loop) assert isinstance(io_event_loop, AbstractEventLoop) self._io_event_loop = io_event_loop self._event_loop_started.release() self._io_event_loop.run_forever()
def run_server(cls, host, port): handler = StaticFilesHandler(get_internal_wsgi_application()) # Save the event loop for the thread in a class variable # so we can unblock it when the tests are finished. cls.server_thread_loop = tulip.new_event_loop() tulip.set_event_loop(cls.server_thread_loop) cls.server_stop = tulip.Future() run(host, port, handler, cls.server_thread_loop, cls.server_stop) cls.server_thread_loop.close()
def setUp(self): super().setUp() self.loop = tulip.new_event_loop() tulip.set_event_loop(self.loop) self.protocol = WebSocketCommonProtocol() self.transport = unittest.mock.Mock() self.transport.close = unittest.mock.Mock( side_effect=lambda: self.protocol.connection_lost(None)) self.protocol.connection_made(self.transport)
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) # heartbeat tulip.Task(self.heartbeat()) tulip.get_event_loop().run_forever() os._exit(0)
def test_use_global_loop(self): with test_utils.run_test_server(self.loop, router=Functional) as httpd: try: tulip.set_event_loop(self.loop) r = self.loop.run_until_complete( client.request('get', httpd.url('method', 'get'))) finally: tulip.set_event_loop(None) content1 = self.loop.run_until_complete(r.read()) content2 = self.loop.run_until_complete(r.read()) content = content1.decode() self.assertEqual(r.status, 200) self.assertIn('"method": "GET"', content) self.assertEqual(content1, content2) r.close()
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) # heartbeat self.heartbeat() tulip.get_event_loop().run_forever() os._exit(0)
def run(addr, port, wsgi_handler, **options): """ Alternate version of django.core.servers.basehttp.run running on Tulip. """ loop = tulip.get_event_loop() if loop is None: # The auto-reloader runs in the main thread and starts the server # in another thread. Create an event loop for that thread. loop = tulip.new_event_loop() tulip.set_event_loop(loop) # The code that reads environ['wsgi.input'] is deep inside Django and hard # to make asynchronous. Pre-loading the payload is the simplest option. protocol_factory = lambda: tulip.http.WSGIServerHttpProtocol( wsgi_handler, readpayload=True) loop.start_serving(protocol_factory, addr, port) loop.run_forever()
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(None) self.wsgi = unittest.mock.Mock() self.stream = unittest.mock.Mock() self.transport = unittest.mock.Mock() self.transport.get_extra_info.return_value = '127.0.0.1' self.headers = [] self.message = protocol.RawRequestMessage( 'GET', '/path', (1, 0), self.headers, True, 'deflate') self.payload = tulip.DataBuffer() self.payload.feed_data(b'data') self.payload.feed_data(b'data') self.payload.feed_eof()
def run(addr, port, wsgi_handler, ipv6=False, threading=False): """ Alternate version of django.core.servers.basehttp.run running on Tulip. """ assert not ipv6, "IPv6 isn't supported" assert threading, "threading is irrelevant" httpd = WSGIServer(addr, port, WSGIRequestHandler) httpd.set_app(wsgi_handler) # The auto-reloader runs in the main thread and starts the server in # another thread. Since the event loop in thread-local we must create one. loop = tulip.get_event_loop() if loop is None: loop = tulip.new_event_loop() tulip.set_event_loop(loop) loop.start_serving(httpd, addr, port) loop.run_forever()
def run(addr, port, wsgi_handler, loop=None, stop=None, **options): """ Alternate version of django.core.servers.basehttp.run running on Tulip. """ if loop is None: loop = tulip.new_event_loop() tulip.set_event_loop(loop) # The code that reads environ['wsgi.input'] is deep inside Django and hard # to make asynchronous. Pre-loading the payload is the simplest option. protocol_factory = lambda: tulip.http.WSGIServerHttpProtocol( wsgi_handler, readpayload=True) sockets = loop.start_serving(protocol_factory, addr, port) if stop is None: loop.run_forever() else: loop.run_until_complete(stop) for socket in sockets: loop.stop_serving(socket)
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) f = loop.start_serving( self.protocol_factory, sock=self.sock, ssl=self.ssl) x = loop.run_until_complete(f)[0] print('Starting srv worker process {} on {}'.format( os.getpid(), x.getsockname())) # heartbeat self.heartbeat() tulip.get_event_loop().run_forever() os._exit(0)
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) f = loop.start_serving( lambda: HttpServer(debug=True, keep_alive=75), sock=self.sock) x = loop.run_until_complete(f)[0] print('Starting srv worker process {} on {}'.format( os.getpid(), x.getsockname())) # heartbeat self.heartbeat() tulip.get_event_loop().run_forever() os._exit(0)
def main(): args = ARGS.parse_args() if ':' in args.host: args.host, port = args.host.split(':', 1) args.port = int(port) if args.iocp: from tulip import windows_events sys.argv.remove('--iocp') logging.info('using iocp') el = windows_events.ProactorEventLoop() tulip.set_event_loop(el) if args.ssl: here = os.path.join(os.path.dirname(__file__), 'tests') if args.certfile: certfile = args.certfile or os.path.join(here, 'sample.crt') keyfile = args.keyfile or os.path.join(here, 'sample.key') else: certfile = os.path.join(here, 'sample.crt') keyfile = os.path.join(here, 'sample.key') sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) sslcontext.load_cert_chain(certfile, keyfile) else: sslcontext = None loop = tulip.get_event_loop() f = loop.start_serving( lambda transport: HttpServer(transport, debug=True, keep_alive=75), args.host, args.port, ssl=sslcontext) socks = loop.run_until_complete(f) print('serving on', socks[0].getsockname()) try: loop.run_forever() except KeyboardInterrupt: pass
def main(): args = ARGS.parse_args() if ':' in args.host: args.host, port = args.host.split(':', 1) args.port = int(port) if args.iocp: from tulip import windows_events sys.argv.remove('--iocp') logging.info('using iocp') el = windows_events.ProactorEventLoop() tulip.set_event_loop(el) if args.ssl: here = os.path.join(os.path.dirname(__file__), 'tests') if args.certfile: certfile = args.certfile or os.path.join(here, 'sample.crt') keyfile = args.keyfile or os.path.join(here, 'sample.key') else: certfile = os.path.join(here, 'sample.crt') keyfile = os.path.join(here, 'sample.key') sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) sslcontext.load_cert_chain(certfile, keyfile) else: sslcontext = None loop = tulip.get_event_loop() f = loop.start_serving( lambda: HttpServer(debug=True, keep_alive=75), args.host, args.port, ssl=sslcontext) socks = loop.run_until_complete(f) print('serving on', socks[0].getsockname()) try: loop.run_forever() except KeyboardInterrupt: pass
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) f = loop.start_serving(lambda: HttpServer(debug=True, keep_alive=75), sock=self.sock) x = loop.run_until_complete(f)[0] print('Starting srv worker process {} on {}'.format( os.getpid(), x.getsockname())) # heartbeat self.heartbeat() tulip.get_event_loop().run_forever() os._exit(0)
def run(self, host='127.0.0.1', port=8000, call_soon=None): if sys.platform == 'win32': from tulip.windows_events import ProactorEventLoop loop = ProactorEventLoop() tulip.set_event_loop(loop) else: loop = tulip.get_event_loop() self.loop = loop task = loop.start_serving(lambda: HttpServer(debug=True, app=self), host, port) socks = loop.run_until_complete(task) if call_soon: loop.call_soon(call_soon) print('Serving on', socks[0].getsockname()) try: loop.run_forever() except KeyboardInterrupt: loop.close()
def run(self, host='127.0.0.1', port=8000, call_soon=None): if sys.platform == 'win32': from tulip.windows_events import ProactorEventLoop loop = ProactorEventLoop() tulip.set_event_loop(loop) else: loop = tulip.get_event_loop() self.loop = loop task = loop.start_serving( lambda: HttpServer(debug=True, app=self), host, port) socks = loop.run_until_complete(task) if call_soon: loop.call_soon(call_soon) print('Serving on', socks[0].getsockname()) try: loop.run_forever() except KeyboardInterrupt: loop.close()
def start(self): # start server self.loop = loop = tulip.new_event_loop() tulip.set_event_loop(loop) def stop(): self.loop.stop() os._exit(0) loop.add_signal_handler(signal.SIGINT, stop) f = loop.start_serving(self.protocol_factory, sock=self.sock, ssl=self.ssl) x = loop.run_until_complete(f)[0] print('Starting srv worker process {} on {}'.format( os.getpid(), x.getsockname())) # heartbeat self.heartbeat() tulip.get_event_loop().run_forever() os._exit(0)
def tearDown(self): tulip.set_event_loop(None) self.loop.close()
def setUp(self): tulip.set_event_loop(None)
print('>>>', cmd.decode('ascii').rstrip()) stdin.write(cmd) # get and print lines from stdout, stderr timeout = None while registered: done, pending = yield from tulip.wait( registered, timeout=timeout, return_when=tulip.FIRST_COMPLETED) if not done: break for f in done: stream = registered.pop(f) res = f.result() print(name[stream], res.decode('ascii').rstrip()) if res != b'': registered[tulip.Task(stream.readline())] = stream timeout = 0.0 stdout_transport.close() stderr_transport.close() if __name__ == '__main__': if sys.platform == 'win32': loop = ProactorEventLoop() tulip.set_event_loop(loop) else: loop = tulip.get_event_loop() loop.run_until_complete(main(loop)) loop.close()
def setUp(self): self.transport = unittest.mock.Mock() tulip.set_event_loop(None)
def mainloop(self): set_event_loop(self) try: self.run_forever() finally: set_event_loop(None)
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(self.loop) self.stream = tulip.StreamReader()
def setUp(self): self.loop = windows_events.ProactorEventLoop() tulip.set_event_loop(None)
def setUp(self): tulip.set_event_loop(None) self.parser = protocol.HttpParser(8190, 32768, 8190)
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(None)
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(None) self.transport = unittest.mock.Mock() self.stream = tulip.StreamBuffer(loop=self.loop)
def setUp(self): self.event_loop = tulip.new_event_loop() tulip.set_event_loop(self.event_loop) self.server = HttpServer(HttpClientFunctional, self.event_loop) self.server.start()
def setUp(self): self.lines_parser = parsers.LinesParser() self.loop = tulip.new_event_loop() tulip.set_event_loop(None)
print(msg.data.strip()) elif msg.tp == websocket.MSG_CLOSE: break yield from dispatch() ARGS = argparse.ArgumentParser( description="websocket console client for wssrv.py example.") ARGS.add_argument( '--host', action="store", dest='host', default='127.0.0.1', help='Host name') ARGS.add_argument( '--port', action="store", dest='port', default=8080, type=int, help='Port number') if __name__ == '__main__': args = ARGS.parse_args() if ':' in args.host: args.host, port = args.host.split(':', 1) args.port = int(port) url = 'http://{}:{}'.format(args.host, args.port) loop = tulip.SelectorEventLoop(tulip.selectors.SelectSelector()) tulip.set_event_loop(loop) loop.add_signal_handler(signal.SIGINT, loop.stop) tulip.Task(start_client(loop, url)) loop.run_forever()
def setUp(self): self.loop = tulip.new_event_loop() tulip.set_event_loop(self.loop) self.start_server()