我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用eventlet.Timeout()。
def get_normal_title(self,url): return_message = "" head = requests.head(url) max_size = 5e6 if 'content-length' in head.headers and int(head.headers['content-length']) > max_size: return_message = "File too big for link preview\r\n" else: with eventlet.Timeout(60, False): response = requests.get(url,timeout=30) if response.status_code == 200: if 'text/html' in response.headers['content-type']: soup = BeautifulSoup(response.content,"lxml") if soup.title is not None: return_message += soup.title.string + "\r\n" else: return_message += response.headers['content-type'] + " Size: " + response.headers['content-length'] + "\r\n" return return_message
def run(self): acceptors = [] for sock in self.sockets: gsock = GreenSocket(sock) gsock.setblocking(1) hfun = partial(self.handle, gsock) acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun, self.worker_connections) acceptors.append(acceptor) eventlet.sleep(0.0) while self.alive: self.notify() eventlet.sleep(1.0) self.notify() try: with eventlet.Timeout(self.cfg.graceful_timeout) as t: [a.kill(eventlet.StopServe()) for a in acceptors] [a.wait() for a in acceptors] except eventlet.Timeout as te: if te != t: raise [a.kill() for a in acceptors]
def parent(signal_path, pid): eventlet.Timeout(5) port = None while True: try: contents = open(signal_path, 'rb').read() port = int(contents.strip()) break except Exception: eventlet.sleep(0.1) eventlet.connect(('127.0.0.1', port)) while True: try: contents = open(signal_path, 'rb').read() result = contents.split()[1] break except Exception: eventlet.sleep(0.1) assert result == b'done', repr(result) print('pass')
def test_parent(self): """ Checks that a terminating greenthread whose parent was a previous, now-defunct hub greenlet returns execution to the hub runloop and not the hub greenlet's parent. """ hub = hubs.get_hub() def dummyproc(): pass g = eventlet.spawn(dummyproc) assert hub.greenlet.parent == eventlet.greenthread.getcurrent() self.assertRaises(KeyboardInterrupt, hub.greenlet.throw, KeyboardInterrupt()) assert not g.dead # check dummyproc hasn't completed with eventlet.Timeout(0.5, self.CustomException()): # we now switch to the hub which will allow # completion of dummyproc. # this should return execution back to the runloop and not # this greenlet so that hub.switch() would block indefinitely. self.assertRaises(self.CustomException, hub.switch) assert g.dead # sanity check that dummyproc has completed
def test_socket_file_read_non_int(): listen_socket = eventlet.listen(('localhost', 0)) def server(): conn, _ = listen_socket.accept() conn.recv(1) conn.sendall(b'response') conn.close() eventlet.spawn(server) sock = eventlet.connect(listen_socket.getsockname()) fd = sock.makefile('rwb') fd.write(b'?') fd.flush() with eventlet.Timeout(1): try: fd.read("This shouldn't work") assert False except TypeError: pass
def test_putting_to_queue(self): timer = eventlet.Timeout(0.1) try: size = 2 self.pool = IntPool(min_size=0, max_size=size) queue = Queue() results = [] def just_put(pool_item, index): self.pool.put(pool_item) queue.put(index) for index in six.moves.range(size + 1): pool_item = self.pool.get() eventlet.spawn(just_put, pool_item, index) for _ in six.moves.range(size + 1): x = queue.get() results.append(x) self.assertEqual(sorted(results), list(six.moves.range(size + 1))) finally: timer.cancel()
def test_select_mark_file_as_reopened(): # https://github.com/eventlet/eventlet/pull/294 # Fix API inconsistency in select and Hub. # mark_as_closed takes one argument, but called without arguments. # on_error takes file descriptor, but called with an exception object. s = original_socket.socket() s.setblocking(0) s.bind(('127.0.0.1', 0)) s.listen(5) gt = eventlet.spawn(select.select, [s], [s], [s]) eventlet.sleep(0.01) with eventlet.Timeout(0.5) as t: with tests.assert_raises(hubs.IOClosed): hubs.get_hub().mark_as_reopened(s.fileno()) gt.wait() t.cancel()
def test_close_idle(self): pool = eventlet.GreenPool() # use log=stderr when test runner can capture it self.spawn_server(custom_pool=pool, log=sys.stdout) connect = ( 'GET /echo HTTP/1.1', 'Upgrade: WebSocket', 'Connection: Upgrade', 'Host: %s:%s' % self.server_addr, 'Origin: http://%s:%s' % self.server_addr, 'Sec-WebSocket-Protocol: ws', 'Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5', 'Sec-WebSocket-Key2: 12998 5 Y3 1 .P00', ) sock = eventlet.connect(self.server_addr) sock.sendall(six.b('\r\n'.join(connect) + '\r\n\r\n^n:ds[4U')) sock.recv(1024) sock.sendall(b'\x00hello\xff') result = sock.recv(1024) assert result, b'\x00hello\xff' self.killer.kill(KeyboardInterrupt) with eventlet.Timeout(1): pool.waitall()
def test_chunked_readline_wsgi_override_minimum_chunk_size(self): fd = self.connect() fd.sendall(b"POST /yield_spaces/override_min HTTP/1.1\r\nContent-Length: 0\r\n\r\n") resp_so_far = b'' with eventlet.Timeout(.1): while True: one_byte = fd.recv(1) resp_so_far += one_byte if resp_so_far.endswith(b'\r\n\r\n'): break self.assertEqual(fd.recv(1), b' ') try: with eventlet.Timeout(.1): fd.recv(1) except eventlet.Timeout: pass else: assert False self.yield_next_space = True with eventlet.Timeout(.1): self.assertEqual(fd.recv(1), b' ')
def test_chunked_readline_wsgi_not_override_minimum_chunk_size(self): fd = self.connect() fd.sendall(b"POST /yield_spaces HTTP/1.1\r\nContent-Length: 0\r\n\r\n") resp_so_far = b'' try: with eventlet.Timeout(.1): while True: one_byte = fd.recv(1) resp_so_far += one_byte if resp_so_far.endswith(b'\r\n\r\n'): break self.assertEqual(fd.recv(1), b' ') except eventlet.Timeout: pass else: assert False
def test_001_trampoline_timeout(self): server_sock = eventlet.listen(('127.0.0.1', 0)) bound_port = server_sock.getsockname()[1] def server(sock): client, addr = sock.accept() eventlet.sleep(0.1) server_evt = eventlet.spawn(server, server_sock) eventlet.sleep(0) try: desc = eventlet.connect(('127.0.0.1', bound_port)) hubs.trampoline(desc, read=True, write=False, timeout=0.001) except eventlet.Timeout: pass # test passed else: assert False, "Didn't timeout" server_evt.wait() check_hub()
def pytest_configure(config): config.addinivalue_line("markers", "example1: use example1 for setup") config.addinivalue_line("markers", "example2: use example2 for setup") config.addinivalue_line("markers", "timeout(N): stop test function " "after N seconds, throwing a Timeout.")
def pytest_pyfunc_call(__multicall__, pyfuncitem): try: timeout = pyfuncitem.obj.timeout.args[0] except (AttributeError, IndexError): timeout = 5.0 with eventlet.Timeout(timeout): return __multicall__.execute()
def test_pyfuncall(): class MC: def execute(self): eventlet.sleep(5.0) class pyfuncitem: class obj: class timeout: args = [0.001] pytest.raises(eventlet.Timeout, lambda: pytest_pyfunc_call(MC(), pyfuncitem))
def test_hang(testdir): p = py.path.local(__file__).dirpath('conftest.py') p.copy(testdir.tmpdir.join(p.basename)) t = testdir.makepyfile(""" import pytest from eventlet.green import time @pytest.mark.timeout(0.01) def test_hang(): time.sleep(3.0) """) result = testdir.runpytest() assert "failed to timeout" not in result.stdout.str() result.stdout.fnmatch_lines(["*Timeout: 0.01*"])
def execute_run(self, exec_id, command): with docker_utils.docker_client() as docker: try: with eventlet.Timeout(CONF.docker.execute_timeout): output = docker.exec_start(exec_id, False, False, False) except eventlet.Timeout: raise exception.Conflict(_( "Timeout on executing command: %s") % command) inspect_res = docker.exec_inspect(exec_id) return {"output": output, "exit_code": inspect_res['ExitCode'], "exec_id": None, "url": None}
def wait_for_registrations(container, number_of_registrations): if not container.started: raise WampyError( "Cannot look for registrations unless the service is running" ) for ext in container.extensions: if type(ext) == WampCalleeProxy: break else: raise WampyError( "no clients found registering callees" ) session = ext.client.session success = False with eventlet.Timeout(TIMEOUT, False): while ( len(session.registration_map.keys()) < number_of_registrations ): eventlet.sleep() success = True if not success: logger.error( "%s has not registered %s callees", ext.client.name, number_of_registrations ) raise WampyError( "Registrations Not Found: {}".format( session.registration_map.keys() ) ) logger.info("found registrations: %s", session.registration_map.keys())
def wait_for_subscriptions(container, number_of_subscriptions): if not container.started: raise WampyError( "Cannot look for registrations unless the service is running" ) for ext in container.extensions: if type(ext) == WampTopicProxy: break else: raise WampyError( "no clients found subscribing to topics" ) session = ext.client.session success = False with eventlet.Timeout(TIMEOUT, False): while ( len(session.subscription_map.keys()) < number_of_subscriptions ): eventlet.sleep() success = True if not success: logger.error( "%s has not subscribed to %s topics", ext.client.name, number_of_subscriptions ) raise WampyError("Subscriptions Not Found") logger.info("found subscriptions: %s", session.subscription_map.keys())
def timeout_ctx(self): return eventlet.Timeout(self.cfg.keepalive or None, False)
def run(self): acceptors = [] for sock in self.sockets: gsock = GreenSocket(sock) gsock.setblocking(1) hfun = partial(self.handle, gsock) acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun, self.worker_connections) acceptors.append(acceptor) eventlet.sleep(0.0) while self.alive: self.notify() try: eventlet.sleep(1.0) except AssertionError: self.alive = False break self.notify() try: with eventlet.Timeout(self.cfg.graceful_timeout) as t: [a.kill(eventlet.StopServe()) for a in acceptors] [a.wait() for a in acceptors] except eventlet.Timeout as te: if te != t: raise [a.kill() for a in acceptors]
def get_data(vk_group): #timeout=eventlet.Timeout(10) try: feed=requests.get(URL_VK%vk_group) return feed.json() except eventlet.timeout.Timeout: logging.warning('Got Timeout while retrieving VK JSON data.cancelling...') return None #finally: #timeout.cancel()
def do_etcd_update(self, etcd_writer, k, v): with eventlet.Timeout(cfg.CONF.ml2_vpp.etcd_write_time, False): if v is None: etcd_writer.delete(k) else: etcd_writer.write(k, v)
def connect(fname, url_to_scrape): """Connect to the given url, Return HTML content Use the text file with name fname for the parsed HTML Return the parsed HTML content as a string Return 'Exit' if connection failure occurs 3 times """ try: with eventlet.Timeout(12, Exception): r = requests.get(url_to_scrape, verify = False) soup = BeautifulSoup(r.text, "html.parser") with open(fname, "w") as text_file: text_file.write("{}".format(soup)) with open(fname) as f: content = f.readlines() connect.counter = 0 return content #except requests.exceptions.RequestException: except Exception: # Static var storing then number of times attempt to connect has failed # If >=4, then we assume that user is not connected to the internet. connect.counter += 1 if connect.counter >= 4: connect.counter = 0 print '\a' print '\nYou don\'t seem to be having internet connectivity | Connection with ERP performance page cannot be established.' reconnect_choice = raw_input('Enter r to try again, x to exit : ') while reconnect_choice not in ['r', 'x']: reconnect_choice = raw_input('Invalid choice! Please enter r to try reconnecting again, or enter x to exit : ') if reconnect_choice == 'r': print 'Retrying....' return connect(fname, url_to_scrape) else: print "\nExiting...." exit(0) else: if connect.counter == 1: print 'Experiencing slow internet connectivity...' # print 'Connection Error' # print 'Retrying....' return connect(fname, url_to_scrape)
def direct_put_to_swifthlm_account(self, container, obj, headers): """ :param container: a container name in SWIFTHLM_ACCOUNT :param obj: a object name :param headers: a dict of headers :returns: the request does succeed or not """ def _check_success(*args, **kwargs): try: direct_put_container_object(*args, **kwargs) return 1 except (ClientException, Timeout, socket.error): return 0 pile = GreenPile() part, nodes = self.container_ring.get_nodes( SWIFTHLM_ACCOUNT, container) for node in nodes: pile.spawn(_check_success, node, part, SWIFTHLM_ACCOUNT, container, obj, headers=headers, conn_timeout=5, response_timeout=15) successes = sum(pile) if successes >= quorum_size(len(nodes)): return True else: return False