def test_multi_file(server, files, dst): result = [] start = time.time() c = PyABS(server) code, msg = c.login(timeout=3) if code == SUCCESS: exec_task(c) tasks = [{ 'client': c, 'filename': filename, 'dst': dst } for filename in files] else: tasks = [] logger.error(msg) try: with Timeout(600, TimeOutException) as timeout: for t in TaskPool.imap_unordered(sftp_task, tasks): logger.info('%s, %s', t, time.time()) result.append(t) except TimeOutException, e: logger.error('*************time out*************')
def _run(self): # pylint: disable=method-hidden stop = None while stop is None: keys_to_remove = [] for key, queue in self.protocol.address_queue.iteritems(): receiver_address = key[0] token_address = key[1] if queue.empty(): elapsed_time = ( time.time() - self.protocol.last_received_time[receiver_address]) # Add a randomized delay in the loop to not clog the network gevent.sleep(randint(0, int(0.2 * self.send_ping_time))) if elapsed_time > self.max_unresponsive_time: # remove the node from the graph token_manager = self.raiden.get_manager_by_token_address( token_address) token_manager.channelgraph.remove_path( self.protocol.raiden.address, receiver_address) # remove the node from the queue keys_to_remove.append(key) elif elapsed_time > self.send_ping_time: self.protocol.send_ping(receiver_address) for key in keys_to_remove: self.protocol.address_queue.pop(key) self.timeout = Timeout( self.sleep_time) # wait() will call cancel() stop = self.stop_event.wait(self.timeout)
def test_multi_server(pyabs, filename, dst): result = [] start = time.time() logger.info('test start: %s', start) # ============================================================ def mapper(server): c = PyABS(server) code, msg = c.login(timeout=3) if code == SUCCESS: exec_task(c) return {'client': c, 'filename': filename, 'dst': dst} else: logger.error(msg) tasks = map(mapper, pyabs) try: with Timeout(600, TimeOutException) as timeout: for t in TaskPool.imap_unordered(sftp_task, tasks): print t, time.time() result.append(t) except TimeOutException, e: logger.error('*************time out*************')
def get(self, block=True, timeout=None): """Return the stored value or raise the exception. If this instance already holds a value or an exception, return or raise it immediately. Otherwise, block until another greenlet calls :meth:`set` or :meth:`set_exception` or until the optional timeout occurs. When the *timeout* argument is present and not ``None``, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). If the *timeout* elapses, the *Timeout* exception will be raised. :keyword bool block: If set to ``False`` and this instance is not ready, immediately raise a :class:`Timeout` exception. """ if self._value is not _NONE: return self._value if self._exc_info: return self._raise_exception() if not block: # Not ready and not blocking, so immediately timeout raise Timeout() # Wait, raising a timeout that elapses self._wait_core(timeout, ()) # by definition we are now ready return self.get(block=False)
def handle(self): """Handle data from the client. Treat timeouts inside the handle method""" # Set the timeout self._timeout = Timeout(self.timeout) self._timeout.start() # Try to handle the request try: while not self.closed.is_set(): # Check if the current client was already routed to a target service if self.server.clients[self.client_address].routed: # In that case we should treat the request as routed, # and do not handle further packets from this client self.handle_routed() break else: # Otherwise, we should expect for a route request within the timeout # defined. Receive and store the packet self.packet = self.request.recv() # Pass the control to the handle_data function self.handle_data() except Timeout as t: # If this is another timeout, raise it so another block can # catch it if t is not self._timeout: raise t self.handle_timeout() finally: self._timeout.cancel()
def _run(self): # pylint: disable=method-hidden stop = None while stop is None: filter_changes = self.filter_.changes() for log_event in filter_changes: log.debug('New Events', task=self.listener_name) event = self.contract_translator.decode_event( log_event['topics'], log_event['data'], ) if event is not None: originating_contract = log_event['address'] try: self.callback(originating_contract, event) except: log.exception('unexpected exception on log listener') self.timeout = Timeout( self.sleep_time) # wait() will call cancel() stop = self.stop_event.wait(self.timeout)
def connect(self): super(OutboundEventSocket, self).connect() # Starts event handler for this client/session. self.start_event_handler() # Sends connect and sets timeout while connecting. timer = Timeout(self.transport.get_connect_timeout()) timer.start() try: connect_response = self._protocol_send("connect") if not connect_response.is_success(): raise ConnectError("Error while connecting") except Timeout: raise ConnectError("Timeout connecting") finally: timer.cancel() # Sets channel and channel unique id from this event self._channel = connect_response self._uuid = connect_response.get_header("Unique-ID") # Set connected flag to True self.connected = True # Sets event filter or raises ConnectError if self._filter: if self._is_eventjson: self.trace("using eventjson") filter_response = self.eventjson(self._filter) else: self.trace("using eventplain") filter_response = self.eventplain(self._filter) if not filter_response.is_success(): raise ConnectError("Event filter failure")
def official_server_fetch(self): """fetch result from official site""" timeout = 7 with Timeout(timeout, False): _, result = Spider(timeout=timeout).deploy(self.phrase) if result: self.racer_weapon(result, gun='official')
def test_channel_withdraw( raiden_network, number_of_nodes, token_addresses, deposit, network_wait, retry_timeout ): """ Withdraw funds after a mediated transfer.""" alice_app, bob_app = raiden_network token_address = token_addresses[0] token_network_address = views.get_token_network_address_by_token_address( views.state_from_app(alice_app), alice_app.raiden.default_registry.address, token_address ) assert token_network_address token_proxy = bob_app.raiden.proxy_manager.token(token_address) bob_initial_balance = token_proxy.balance_of(bob_app.raiden.address) message_handler = WaitForMessage() bob_app.raiden.message_handler = message_handler alice_to_bob_amount = 10 identifier = 1 target = bob_app.raiden.address secret = sha3(target) payment_status = alice_app.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=alice_to_bob_amount, target=target, identifier=identifier, secret=secret, ) wait_for_unlock = bob_app.raiden.message_handler.wait_for_message( Unlock, {"payment_identifier": identifier} ) timeout = network_wait * number_of_nodes with Timeout(seconds=timeout): wait_for_unlock.get() msg = ( f"transfer from {to_checksum_address(alice_app.raiden.address)} " f"to {to_checksum_address(bob_app.raiden.address)} failed." ) assert payment_status.payment_done.get(), msg total_withdraw = deposit + alice_to_bob_amount bob_alice_channel_state = get_channelstate(bob_app, alice_app, token_network_address) bob_app.raiden.withdraw( canonical_identifier=bob_alice_channel_state.canonical_identifier, total_withdraw=total_withdraw, ) waiting.wait_for_withdraw_complete( raiden=bob_app.raiden, canonical_identifier=bob_alice_channel_state.canonical_identifier, total_withdraw=total_withdraw, retry_timeout=retry_timeout, ) bob_balance_after_withdraw = token_proxy.balance_of(bob_app.raiden.address) assert bob_initial_balance + total_withdraw == bob_balance_after_withdraw
def poll_all_received_events(self): """ This will be triggered once for each `echo_node_alarm_callback`. It polls all channels for `EventTransferReceivedSuccess` events, adds all new events to the `self.received_transfers` queue and respawns `self.echo_node_worker`, if it died. """ locked = False try: with Timeout(10): locked = self.lock.acquire(blocking=False) if not locked: return else: channels = self.api.get_channel_list( registry_address=self.api.raiden.default_registry. address, token_address=self.token_address, ) received_transfers = list() for channel_state in channels: channel_events = self.api.get_channel_events( channel_state.token_network_identifier, channel_state.identifier, self.last_poll_block, ) received_transfers.extend([ event for event in channel_events if event['event'] == 'EventTransferReceivedSuccess' ]) for event in received_transfers: transfer = event.copy() transfer.pop('block_number') self.received_transfers.put(transfer) # set last_poll_block after events are enqueued (timeout safe) if received_transfers: self.last_poll_block = max( event['block_number'] for event in received_transfers) # increase last_poll_block if the blockchain proceeded delta_blocks = self.api.raiden.get_block_number( ) - self.last_poll_block if delta_blocks > 1: self.last_poll_block += 1 if not self.echo_worker_greenlet.started: log.debug( 'restarting echo_worker_greenlet', dead=self.echo_worker_greenlet.dead, successful=self.echo_worker_greenlet.successful(), exception=self.echo_worker_greenlet.exception, ) self.echo_worker_greenlet = gevent.spawn( self.echo_worker) except Timeout: log.info('timeout while polling for events') finally: if locked: self.lock.release()
def test_web_rtc_message_sync(matrix_transports): transport0, transport1 = matrix_transports transport1_messages = set() raiden_service0 = MockRaidenService() raiden_service1 = MockRaidenService() def mock_handle_web_rtc_messages(message_data, partner_address): messages = validate_and_parse_message(message_data, partner_address) transport1_messages.update(messages) # set mock function to make sure messages are sent via web rtc transport1._web_rtc_manager._handle_message_callback = mock_handle_web_rtc_messages transport0.start(raiden_service0, [], None) transport1.start(raiden_service1, [], None) transport0.immediate_health_check_for(transport1._raiden_service.address) transport1.immediate_health_check_for(transport0._raiden_service.address) with Timeout(TIMEOUT_WEB_RTC_CONNECTION): # wait until web rtc connection is ready while not transport0._web_rtc_manager.has_ready_channel(raiden_service1.address): gevent.sleep(1) while not transport1._web_rtc_manager.has_ready_channel(raiden_service0.address): gevent.sleep(1) queue_identifier = QueueIdentifier( recipient=transport1._raiden_service.address, canonical_identifier=factories.UNIT_CANONICAL_ID, ) raiden0_queues = views.get_all_messagequeues(views.state_from_raiden(raiden_service0)) raiden0_queues[queue_identifier] = [] for i in range(5): message = Processed(message_identifier=MessageID(i), signature=EMPTY_SIGNATURE) raiden0_queues[queue_identifier].append(message) transport0._raiden_service.sign(message) transport0.send_async([MessagesQueue(queue_identifier, [message])]) with Timeout(TIMEOUT_MESSAGE_RECEIVE): while not len(transport1_messages) == 5: gevent.sleep(0.1)
def _get_instances(cluster): t = Timeout(RAPI_TIMEOUT) t.start() try: instances.extend(cluster.get_instances()) except (GanetiApiError, Timeout): pass finally: t.cancel()
def _get_instances(cluster): t = Timeout(RAPI_TIMEOUT) t.start() try: instances.extend(cluster.get_user_instances(request.user)) except (GanetiApiError, Timeout): bad_clusters.append(cluster) finally: t.cancel()
def code(): if _local.db_conn.charset != charset: _local.db_conn.set_charset(charset) with _local.db_conn.cursor() as cursor: with Timeout(db_config.query_timeout): cursor.execute(sql, values) return cursor.fetchall(), cursor.rowcount
def server_is_alive(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: with Timeout(1.0): s.connect((self.remote.hostname, self.remote.port)) except (socket.error, Timeout): return False finally: s.close() return True
def callRemoteForResult(self, _name, *args, **kw): """执行远程调用,并等待结果 @param _name: 调用的远程方法的名称 @param timeout: int 结果返回的默认超时时间 @param args: 远程方法需要的参数 @param kw: 远程方法需要的默认参数 """ _key, result = AsyncResultFactory().createAsyncResult() self.broker._sendMessage(_key, _name, args, kw) return result.get(timeout=Timeout(self.timeout))
def purge(self, queue): """ Deletes all contents of a queue. @TODO could end up in a race with an infinite producer """ assert queue in self._queues with Timeout(5): while not self._queues[queue].empty(): self._queues[queue].get_nowait()
def poll_all_received_events(self): """ This will be triggered once for each `echo_node_alarm_callback`. It polls all channels for `EventPaymentReceivedSuccess` events, adds all new events to the `self.received_transfers` queue and respawns `self.echo_node_worker`, if it died. """ locked = False try: with Timeout(10): locked = self.lock.acquire(blocking=False) if not locked: return else: received_transfers = self.api.get_payment_history_for_token( self.token_address, from_block=self.last_poll_block, ) # received transfer is a tuple of (block_number, event) received_transfers = [ (block_number, event) for block_number, event in received_transfers if type(event) == EventPaymentReceivedSuccess ] for _, event in received_transfers: transfer = copy.deepcopy(event) self.received_transfers.put(transfer) # set last_poll_block after events are enqueued (timeout safe) if received_transfers: self.last_poll_block = max( block_number for block_number, _ in received_transfers) # increase last_poll_block if the blockchain proceeded delta_blocks = self.api.raiden.get_block_number( ) - self.last_poll_block if delta_blocks > 1: self.last_poll_block += 1 if not self.echo_worker_greenlet.started: log.debug( 'restarting echo_worker_greenlet', dead=self.echo_worker_greenlet.dead, successful=self.echo_worker_greenlet.successful(), exception=self.echo_worker_greenlet.exception, ) self.echo_worker_greenlet = gevent.spawn( self.echo_worker) except Timeout: log.info('timeout while polling for events') finally: if locked: self.lock.release()
def download(url): data = '' retries = 0 timeout = 30.0 while not data and retries < 10: if retries > 0: print "retry %d ..." % retries with Timeout(timeout, False): data = urllib.urlopen(url).read().decode('cp1251', 'ignore') retries += 1 timeout *= 1.1 return data
def test(): result = [] start = time.time() print 'test start: %s' % start try: with Timeout(2, TimeOutException) as timeout: for t in TaskPool.imap_unordered(sub_task, xrange(10)): print t, time.time() result.append(t) except TimeOutException, e: print '*************time out*************'
def _transfer_expired( initiator_app: App, target_app: App, token_address: TokenAddress, amount: PaymentAmount, identifier: PaymentID, timeout: Optional[float] = None, ) -> SecretHash: assert identifier is not None, "The identifier must be provided" assert isinstance(target_app.raiden.message_handler, WaitForMessage) # This timeout has to be larger then the lock expiration. The lock # expiration unit is block numbers, and its value is defined relative to # the node's reveal timeout configuration. For the integration tests the # reveal timeout is chosen proportionally to the number of nodes, 90 # seconds is a rough default that should work with the standard # configuration. if timeout is None: timeout = 90 secret, secrethash = make_secret_with_hash() wait_for_remove_expired_lock = target_app.raiden.message_handler.wait_for_message( LockExpired, {"secrethash": secrethash} ) token_network_registry_address = initiator_app.raiden.default_registry.address token_network_address = views.get_token_network_address_by_token_address( chain_state=views.state_from_app(initiator_app), token_network_registry_address=token_network_registry_address, token_address=token_address, ) assert token_network_address payment_status = initiator_app.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=amount, target=TargetAddress(target_app.raiden.address), identifier=identifier, secret=secret, secrethash=secrethash, ) with Timeout(seconds=timeout): wait_for_remove_expired_lock.get() msg = ( f"transfer from {to_checksum_address(initiator_app.raiden.address)} " f"to {to_checksum_address(target_app.raiden.address)} did not expire." ) assert payment_status.payment_done.get() is False, msg return secrethash
def close(self): """ Send a *bye* message to the :class:`AnnounceServer` and disconnect. """ timeout = Timeout(1) timeout.start() try: self._announce.bye(self) except Timeout: pass finally: timeout.cancel() self._announce.close() self._mailbox.stop() [gevent.kill(g) for g in (self._greenlet, self._greenlet_ann_sub) if g]
def _wait_auth_request(self): ''' Waits until auth/request event is received. ''' # Sets timeout to wait for auth/request timer = Timeout(self.transport.get_connect_timeout()) timer.start() try: # When auth/request is received, # _auth_request method will wake up async result # so we will just wait this event here. return self._wait_auth_event.get() except Timeout: raise ConnectError("Timeout waiting auth/request") finally: timer.cancel()
def _transfer_unlocked( initiator_app: RaidenService, target_app: RaidenService, token_address: TokenAddress, amount: PaymentAmount, identifier: PaymentID, timeout: Optional[float] = None, expect_unlock_failures: bool = False, route_states: List[RouteState] = None, ) -> SecretHash: assert isinstance(target_app.message_handler, WaitForMessage) if timeout is None: timeout = 10 wait_for_unlock = target_app.message_handler.wait_for_message( Unlock, {"payment_identifier": identifier}) token_network_registry_address = initiator_app.default_registry.address token_network_address = views.get_token_network_address_by_token_address( chain_state=views.state_from_raiden(initiator_app), token_network_registry_address=token_network_registry_address, token_address=token_address, ) assert token_network_address secret, secrethash = make_secret_with_hash() payment_status = initiator_app.mediated_transfer_async( token_network_address=token_network_address, amount=amount, target=TargetAddress(target_app.address), identifier=identifier, secret=secret, secrethash=secrethash, route_states=route_states, ) apps = [initiator_app, target_app] with watch_for_unlock_failures( *apps) if not expect_unlock_failures else nullcontext(): with Timeout(seconds=timeout): wait_for_unlock.get() msg = ( f"transfer from {to_checksum_address(initiator_app.address)} " f"to {to_checksum_address(target_app.address)} failed.") assert payment_status.payment_done.get(), msg return secrethash
def poll_all_received_events(self) -> None: """ This will be triggered once for each `echo_node_alarm_callback`. It polls all channels for `EventPaymentReceivedSuccess` events, adds all new events to the `self.received_transfers` queue and respawns `self.echo_worker`, if it died. """ locked = False try: with Timeout(10): locked = self.lock.acquire(blocking=False) if not locked: return else: received_transfers: List[ Event] = self.api.get_raiden_events_payment_history( token_address=self.token_address, offset=self.num_seen_events) received_transfers = [ event for event in received_transfers if type(event) == EventPaymentReceivedSuccess ] for event in received_transfers: transfer = copy.deepcopy(event) self.received_transfers.put(transfer) # set last_poll_block after events are enqueued (timeout safe) if received_transfers: self.num_seen_events += len(received_transfers) if not bool(self.echo_worker_greenlet): log.debug( "Restarting echo_worker_greenlet", node=to_checksum_address(self.api.address), dead=self.echo_worker_greenlet.dead, successful=self.echo_worker_greenlet.successful(), exception=self.echo_worker_greenlet.exception, ) self.echo_worker_greenlet = gevent.spawn( self.echo_worker) except Timeout: log.info("Timeout while polling for events") finally: if locked: self.lock.release()
def _run_queue_item(self, queue_item): temp_file = os.path.join(self.downloader._tmp_directory, queue_item.filename) try: self._logger.debug("begin download") timeout = Timeout(self.downloader.timeout) timeout.start() try: self._download_file(temp_file, queue_item) finally: timeout.cancel() self._unpack_file(temp_file, queue_item) except NotFound: self._handle_notfound(queue_item) except Timeout: self._handle_timeout(queue_item) except BadFile as e: abort = self._handle_badfile(queue_item) if abort: raise # thereby killing the whole download except (gevent.socket.error, ftplib.Error): self._handle_ioerror(queue_item) except (greenlet.GreenletExit, KeyboardInterrupt, SystemExit): raise else: self._server_sleep_backoff = 0 # unfortunately gevent doesn't have JoinablePriorityQueues self.downloader._file_complete() finally: try: os.unlink(temp_file) except OSError as e: if e.errno != errno.ENOENT: raise
def get(self, block=True, timeout=None): """ get(block=True, timeout=None) -> object Return the result the greenlet has returned or re-raise the exception it has raised. If block is ``False``, raise :class:`gevent.Timeout` if the greenlet is still alive. If block is ``True``, unschedule the current greenlet until the result is available or the timeout expires. In the latter cases, :class:`gevent.Timeout` is raised. """ if self.ready(): if self.successful(): return self.value self._raise_exception() if not block: raise Timeout() switch = getcurrent().switch # pylint:disable=undefined-variable self.rawlink(switch) try: t = Timeout._start_new_or_dummy(timeout) try: result = self.parent.switch() if result is not self: raise InvalidSwitchError( 'Invalid switch into Greenlet.get(): %r' % (result, )) finally: t.cancel() except: # unlinking in 'except' instead of finally is an optimization: # if switch occurred normally then link was already removed in _notify_links # and there's no need to touch the links set. # Note, however, that if "Invalid switch" assert was removed and invalid switch # did happen, the link would remain, causing another invalid switch later in this greenlet. self.unlink(switch) raise if self.ready(): if self.successful(): return self.value self._raise_exception()
def get(self, block=True, timeout=None): """Return the stored value or raise the exception. If this instance already holds a value or an exception, return or raise it immediatelly. Otherwise, block until another greenlet calls :meth:`set` or :meth:`set_exception` or until the optional timeout occurs. When the *timeout* argument is present and not ``None``, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). :keyword bool block: If set to ``False`` and this instance is not ready, immediately raise a :class:`Timeout` exception. """ if self._value is not _NONE: return self._value if self._exc_info: return self._raise_exception() if not block: # Not ready and not blocking, so immediately timeout raise Timeout() switch = getcurrent().switch self.rawlink(switch) try: timer = Timeout._start_new_or_dummy(timeout) try: result = self.hub.switch() if result is not self: raise InvalidSwitchError( 'Invalid switch into AsyncResult.get(): %r' % (result, )) finally: timer.cancel() except: self.unlink(switch) raise # by definition we are now ready return self.get(block=False)
def transfer( initiator_app: App, target_app: App, token_address: TokenAddress, amount: PaymentAmount, identifier: PaymentID, fee: FeeAmount = 0, timeout: float = 10, ) -> None: """ Nice to read shortcut to make a transfer. The transfer is a LockedTransfer and we make sure, all apps are synched. The secret will also be revealed. """ assert identifier is not None, 'The identifier must be provided' assert isinstance(target_app.raiden.message_handler, WaitForMessage) wait_for_unlock = target_app.raiden.message_handler.wait_for_message( Unlock, {'payment_identifier': identifier}, ) payment_network_identifier = initiator_app.raiden.default_registry.address token_network_identifier = views.get_token_network_identifier_by_token_address( chain_state=views.state_from_app(initiator_app), payment_network_id=payment_network_identifier, token_address=token_address, ) payment_status = initiator_app.raiden.mediated_transfer_async( token_network_identifier=token_network_identifier, amount=amount, target=target_app.raiden.address, identifier=identifier, fee=fee, ) with Timeout(seconds=timeout): wait_for_unlock.get() payment_status.payment_done.wait()
def _transfer_secret_not_requested( initiator_app: App, target_app: App, token_address: TokenAddress, amount: PaymentAmount, identifier: PaymentID, timeout: Optional[float] = None, ) -> SecretHash: if timeout is None: timeout = 10 secret, secrethash = make_secret_with_hash() assert isinstance(target_app.raiden.raiden_event_handler, HoldRaidenEventHandler) hold_secret_request = target_app.raiden.raiden_event_handler.hold( SendSecretRequest, {"secrethash": secrethash}) token_network_registry_address = initiator_app.raiden.default_registry.address token_network_address = views.get_token_network_address_by_token_address( chain_state=views.state_from_app(initiator_app), token_network_registry_address=token_network_registry_address, token_address=token_address, ) assert token_network_address initiator_app.raiden.start_mediated_transfer_with_secret( token_network_address=token_network_address, amount=amount, target=TargetAddress(target_app.raiden.address), identifier=identifier, secret=secret, secrethash=secrethash, ) with Timeout(seconds=timeout): hold_secret_request.get() return secrethash