我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用gevent.pywsgi()。
def run_application(self): upgrade_header = self.environ.get('HTTP_UPGRADE', '').lower() if upgrade_header: # Build and start the HTTP response self.environ['ws4py.socket'] = self.socket or self.environ['wsgi.input'].rfile._sock self.result = self.application(self.environ, self.start_response) or [] self.process_result() del self.environ['ws4py.socket'] self.socket = None self.rfile.close() ws = self.environ.pop('ws4py.websocket', None) if ws: ws_greenlet = self.server.pool.track(ws) # issue #170 # in gevent 1.1 socket will be closed once application returns # so let's wait for websocket handler to finish ws_greenlet.join() else: gevent.pywsgi.WSGIHandler.run_application(self)
def run_application(self): upgrade_header = self.environ.get('HTTP_UPGRADE', '').lower() if upgrade_header: try: # Build and start the HTTP response self.environ['ws4py.socket'] = self.socket or self.environ['wsgi.input'].rfile._sock self.result = self.application(self.environ, self.start_response) or [] self.process_result() except: raise else: del self.environ['ws4py.socket'] self.socket = None self.rfile.close() ws = self.environ.pop('ws4py.websocket') if ws: self.server.pool.track(ws) else: gevent.pywsgi.WSGIHandler.run_application(self)
def __init__(self, *args, **kwargs): """ WSGI server that simply tracks websockets and send them a proper closing handshake when the server terminates. Other than that, the server is the same as its :class:`gevent.pywsgi.WSGIServer` base. """ _WSGIServer.__init__(self, *args, **kwargs) self.pool = GEventWebSocketPool()
def setUp(self): super(WebserverTestCase, self).setUp() self._web_server = gevent.pywsgi.WSGIServer(("127.0.0.1", 0), app, log=None) gevent.spawn(lambda: self._web_server.serve_forever()) gevent.sleep(0.01) self.port = self._web_server.server_port global_stats.clear_all()
def main(sample_period, auction_address, chain_name, state_file, host, port): from gevent.pywsgi import WSGIServer app = Flask(__name__) api = Api(app) project = Project() with project.get_chain(chain_name) as chain: Auction = chain.provider.get_contract_factory('DutchAuction') auction_contract = Auction(address=auction_address) sampler = EventSampler(auction_address, chain, state_file_path=state_file) api.add_resource(AuctionStatus, "/status", resource_class_kwargs={'auction_contract': auction_contract, 'sampler': sampler}) rest_server = WSGIServer((host, port), app) server_greenlet = gevent.spawn(rest_server.serve_forever) server_greenlet.join()