def __start_auto_reconnect(self): """ Starts auto reconnection process in background """ if self.__recon is not None: # reconnection is in progress return if not self.autoreconnect: # reconnection is not enabled return #LOG.debug("trying reconnect...") emit("sender-conn-reconn", self._address) self.__recon = gevent.Greenlet(self.connection_loop) self.__recon.start()
def _do_test(self, block, killall, exc=None): link_test = [] result = [] g = gevent.Greenlet(self._run_in_greenlet, result) g.link(link_test.append) self._start_greenlet(g) self.__kill_greenlet(g, block, killall, exc) self._after_kill_greenlet(g) self.assertFalse(result) self.assertEqual(link_test, [g])
def tee_and_handle(f, msgs): queue = Queue() # unbounded buffer def _run(): for msg in msgs: print(COLOR_RECV, 'Received:', COLOR_ENDC, msg.command) if msg.command == b'ping': send(f, msg_pong(nonce=msg.nonce)) queue.put(msg) t = gevent.Greenlet(_run) t.start() while True: yield (queue.get())
def validate_peer(self, peer): #start communication with peer gevent.Greenlet(peer).start() #validate any peer enable to authenticate self._transmit_to_peer(Action(None, "auth", {}, []), peer, 15) if not peer.authed: return False if peer.status == 'unknown': token_tmp = self.__id_generator() self._transmit_to_peer( Action(None, 'external_configuration', {'token_tmp': token_tmp}, []), peer, 15) peer.token = token_tmp return True
def test_simple_exit(self): link_test = [] def func(delay, return_value=4): gevent.sleep(delay) return return_value g = gevent.Greenlet(func, 0.01, return_value=5) g.rawlink( link_test.append ) # use rawlink to avoid timing issues on Appveyor/Travis (not always successful) self.assertFalse(g, g) self.assertFalse(g.dead, g) self.assertFalse(g.started, g) self.assertFalse(g.ready(), g) self.assertFalse(g.successful(), g) self.assertIsNone(g.value, g) self.assertIsNone(g.exception, g) g.start() self.assertTrue(g, g) # changed self.assertFalse(g.dead, g) self.assertTrue(g.started, g) # changed self.assertFalse(g.ready(), g) self.assertFalse(g.successful(), g) self.assertIsNone(g.value, g) self.assertIsNone(g.exception, g) gevent.sleep(0.001) self.assertTrue(g) self.assertFalse(g.dead, g) self.assertTrue(g.started, g) self.assertFalse(g.ready(), g) self.assertFalse(g.successful(), g) self.assertIsNone(g.value, g) self.assertIsNone(g.exception, g) self.assertFalse(link_test) gevent.sleep(0.02) self.assertFalse(g, g) # changed self.assertTrue(g.dead, g) # changed self.assertFalse(g.started, g) # changed self.assertTrue(g.ready(), g) # changed self.assertTrue(g.successful(), g) # changed self.assertEqual(g.value, 5) # changed self.assertIsNone(g.exception, g) self.assertTrue(link_test == [g] or greentest.RUNNING_ON_CI, link_test) # changed
def test_simple_exit(self): link_test = [] def func(delay, return_value=4): gevent.sleep(delay) return return_value g = gevent.Greenlet(func, 0.01, return_value=5) g.rawlink( link_test.append ) # use rawlink to avoid timing issues on Appveyor (not always successful) assert not g, bool(g) assert not g.dead assert not g.started assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None g.start() assert g # changed assert not g.dead assert g.started # changed assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None gevent.sleep(0.001) assert g assert not g.dead assert g.started assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None assert not link_test gevent.sleep(0.02) assert not g assert g.dead assert not g.started assert g.ready() assert g.successful() assert g.value == 5 assert g.exception is None # not changed assert link_test == [ g ] or greentest.RUNNING_ON_APPVEYOR, link_test # changed
def _schedule_service(self, service): """Runs a service job and schedules it to run again every period (interval) specified by the service Arguments: service (BackgroundService): background service object """ greenlet = gevent.Greenlet(service.job) greenlet.link(self.__callback) greenlet.link_exception(self.__on_exception) greenlet.start() self._running[service.name] = greenlet self._running[service.name].service = service next_start = ceil(self.seconds_to_next_interval(service.interval)) self._scheduled[service.name] = gevent.spawn_later( next_start, self._schedule_service, service=service)
def test_multiplecalls(self): cl = j.core.zdaemon.getZDaemonClient('127.0.0.1', port=PORT, category='agent', gevent=True) def makeCall(): cl.echo('test') greenlets = list() for x in xrange(5): grn = gevent.Greenlet(makeCall) grn.start() greenlets.append(grn) gevent.joinall(greenlets, raise_error=True)
def make_thumbnail(image_path): try: img = cv2.imread(image_path) except IOError: if ARGS.baddir: gevent.Greenlet(move_task, os.path.basename(image_path), ARGS.baddir).start() raise IOError('Cannot read [%s]. Moving to [%s]' % (image_path, ARGS.baddir)) else: raise IOError('Cannot read [%s]' % image_path) height, width = img.shape[:2] width = int(width * ARGS.thumbsize / float(height)) img = cv2.resize(img, (width, ARGS.thumbsize)) return cv2.imencode('.jpeg', img)[1].tostring()
def __init__(self, endpoint=None, oauth_session=None, start=True): self.endpoint = endpoint self.oauth_session = \ requests.Session() if oauth_session is None else oauth_session self.shutdown_called = False # Inbound self.message_queue = gevent.queue.Queue() # Outbound self.subscription_queue = gevent.queue.Queue() self.unsubscription_queue = gevent.queue.Queue() self.publication_queue = gevent.queue.Queue() self.channel_ids = {} self.subscription_callbacks = {} # handshake() has a side effect of initializing self.message_counter self.handshake() self.executing = False self.stop_greenlets = False self.outbound_greenlets = [ gevent.Greenlet(self._subscribe_greenlet), gevent.Greenlet(self._unsubscribe_greenlet), gevent.Greenlet(self._publish_greenlet) ] self.inbound_greenlets = [gevent.Greenlet(self._connect_greenlet)] self.greenlets = self.outbound_greenlets + self.inbound_greenlets if start: self.start()
def init_webui(self): """ Initialize and start the flask based webui. This does not check if it is enabled, so that must happen prior to calling this :return: :rtype: """ host = config.get_default('flask', 'listen_host', 'localhost') port = config.get_default('flask', 'listen_port', 5000, int) from tranny.app import create_app self.wsgi_app = create_app() self.wsgi_socketio = SocketIOServer((host, port), self.wsgi_app, resource='socket.io') self.wsgi_server = gevent.Greenlet(self.wsgi_socketio.serve_forever)
def test_run(self): # confirm server is offline with self.assertRaises(requests.exceptions.ConnectionError): requests.get('http://localhost:8080') # start and query server greenlet = gevent.Greenlet(run=self.engine.run) greenlet.start() ret = requests.get('http://localhost:8080') self.assertIsNotNone(ret) # confirm server is offline again gevent.kill(greenlet) with self.assertRaises(requests.exceptions.ConnectionError): requests.get('http://localhost:8080')
class A(object): x = 1 __y = 2 import gevent gevent.Greenlet() def a(self, arg): print self.arg @classmethod def _a(cls): print(cls) print(cls.x) def __a(self): print self.__y
def __init__(self, qualifier=None): self._event_queue = Queue() self.greenlet = gevent.Greenlet(self._loop) self._op_count = 0 self._current_msg = None self.started = False # Message being processed; purely for logging. self.msg_uuid = None # Logging parameters self.qualifier = qualifier if qualifier: self.name = "%s(%s)" % (self.__class__.__name__, qualifier) else: self.name = self.__class__.__name__
def start(self, segment): """Begin transfer for an indicated wal segment.""" if self.closed: raise UserCritical(msg='attempt to transfer wal after closing', hint='report a bug') g = gevent.Greenlet(self.transferer, segment) g.link(self._complete_execution) # Increment .expect before starting the greenlet, or else a # very unlucky .join could be fooled as to when pool is # complete. self.expect += 1 g.start()
def recv_connect(self): print("recieved boss connection") def sendcpu(): lastmode = 0 self.db = current_spectra.current_spectra(mode='r') while True: spectrum, timestamp, mode = self.db.getCurrentSpectrum() spectime = time.localtime(timestamp) spectrum = spectrum[cnf.modes[mode]['low_chan']:cnf. modes[mode]['high_chan'] + 1] timeS = '%02i:%02i:%02i on %02i/%02i/%04i' % ( spectime[3], spectime[4], spectime[5], spectime[2], spectime[1], spectime[0]) miny = np.asscalar(spectrum.min()) maxy = np.asscalar(spectrum.max()) js = json.dumps(spectrum.tolist()) freqs = np.array([]) minx = 0 maxx = 0 if mode != lastmode: print "mode = %i != lastmode = %i" % (mode, lastmode) freqs = cnf.getFreqs(mode)[cnf.modes[mode]['low_chan']:cnf. modes[mode]['high_chan'] + 1] / 10e5 minx = freqs.min() maxx = freqs.max() print "freqs.shape = %s" % str(freqs.shape) lastmode = mode self.emit('data',{'spectrum':js,\ 'freqs':json.dumps(freqs.tolist()),\ 'minx':minx,\ 'maxx':maxx,\ 'maxy':maxy,\ 'miny':miny,\ 'heading':"Spectrum taken at %s"%timeS,\ 'xaxis':"Frequency (Mhz)",\ 'yaxis':"Power (dBm)"}) # print ("sent data for %s"%timeS) gevent.sleep(0.01) print "spawn updater" self.updater = gevent.Greenlet(sendcpu) self.updater.start()
def start(self, group=None): def runloop(): logger.info("Proxy runloop started") http_server = WSGIServer((self.addr, self.port), app) http_server.serve_forever() def start_greenlets(): logger.info("starting Proxy runloop...") self.runloop_greenlet.start() # create greenlets self.runloop_greenlet = gevent.Greenlet(runloop) if group != None: group.add(self.runloop_greenlet) # start greenlets gevent.spawn(start_greenlets)
def test_simple_exit(self): link_test = [] def func(delay, return_value=4): gevent.sleep(delay) return return_value g = gevent.Greenlet(func, 0.01, return_value=5) g.link(lambda x: link_test.append(x)) assert not g, bool(g) assert not g.dead assert not g.started assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None g.start() assert g # changed assert not g.dead assert g.started # changed assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None gevent.sleep(0.001) assert g assert not g.dead assert g.started assert not g.ready() assert not g.successful() assert g.value is None assert g.exception is None assert not link_test gevent.sleep(0.02) assert not g assert g.dead assert not g.started assert g.ready() assert g.successful() assert g.value == 5 assert g.exception is None # not changed assert link_test == [g] # changed
def _sync(self, timeout_ms=30000): """ Reimplements MatrixClient._sync, add 'account_data' support to /sync """ response = self.api.sync(self.sync_token, timeout_ms) prev_sync_token = self.sync_token self.sync_token = response["next_batch"] if self._handle_thread is not None: # if previous _handle_thread is still running, wait for it and re-raise if needed self._handle_thread.get() is_first_sync = (prev_sync_token is None) self._handle_thread = gevent.Greenlet(self._handle_response, response, is_first_sync) self._handle_thread.name = f'sync_handle_response-{prev_sync_token}' self._handle_thread.link_exception(lambda g: self.sync_thread.kill(g.exception)) self._handle_thread.start() if self._post_hook_func is not None: self._post_hook_func(self.sync_token)
def start(self, graph_id, done_callback): """ Execute a graph. """ self.logger.debug('Graph {}: Starting execution'.format(graph_id)) graph = self.get_graph(graph_id) network = Network(graph) executor = gevent.Greenlet(network.go) # FIXME: should we delete the executor from self._executors on finish? # this has an impact on the result returned from get_status(). Leaving # it means that after completion it will be started:True, running:False # until stop() is triggered, at which point it will be started:False, # running:False executor.link(lambda g: done_callback()) self._executors[graph_id] = (executor, network) executor.start()
def queue_connect(service_name, callback=None, wait=False): if not callback: callback = eval(''.join(['check_', service_name, '_login'])) url = eval(''.join(['stlib.', service_name, '_check_page'])) greenlet = gevent.Greenlet(stlib.network.try_get_response, service_name, url) greenlet.link(callback) greenlet.start() if wait: try: greenlet.join() except KeyboardInterrupt: sys.exit(0) return greenlet
def recurring_action(self, action, period): """ configure an action to be executed every period second It will ensure that the action from service is schedule at best every period second. Since we dont' have control over how long other task from the task list take. we can only ensure that the action is never going to be schedule faster then every period second That means that it can be a longer time then period second during which the action is not executed @param action: a method or string that match the name of the method we want to make recurring @param period: minimum number of seconds between 2 scheduling of the action """ if inspect.ismethod(action) or inspect.isfunction(action): action = action.__name__ gl = gevent.Greenlet(_recurring_action, self, action, period) self.gl_mgr.add("recurring_" + action, gl)
def load_events(self): self.state = State.LOADING # support for gevent based dagit server if gevent.get_hub().gr_frame: self.stopping = gevent.event.Event() self.stopped = gevent.event.Event() load_thread = gevent.Greenlet(self.background_event_loading, gevent.sleep) else: self.stopping = Event() self.stopped = Event() load_thread = Thread( target=self.background_event_loading, args=(sleep, ), name=f"load-events-{self.run_id}", ) load_thread.start()
def start(self, group=None): def runloop(): logger.info("Blocks runloop started") while 1: self.block_num = parse_block_txs_easy( self.block_num, self.on_block_transfer_tx, self.node_http_base_url) gevent.sleep(20) def start_greenlet(): logger.info("starting Blocks runloop...") self.runloop_greenlet.start() # create greenlet self.runloop_greenlet = gevent.Greenlet(runloop) if group != None: group.add(self.runloop_greenlet) # start greenlet gevent.spawn(start_greenlet)
def __init__(self): self.feeds = [] self.services = [] self.webui = None self.client = None self.init_db() self._updater = gevent.Greenlet(self.update_providers) self._updater.start_later(1) self.watch = None self.init_providers() self.client = client.init_client() app.torrent_client = self.client self.wsgi_app = None self.wsgi_socketio = None self.wsgi_server = None if config.getboolean("webui", "enabled"): self.init_webui() self.wsgi_server.start()
def __init__(self, qualifier=None): self._event_queue = Queue() self.greenlet = gevent.Greenlet(self._loop) self._op_count = 0 self._current_msg = None self.started = False # Message being processed; purely for logging. self.msg_uuid = None # Logging parameters self.qualifier = qualifier if qualifier: self.name = "%s(%s)" % (self.__class__.__name__, qualifier) else: self.name = self.__class__.__name__ # Can't use str(self) yet, it might not be ready until subclass # constructed. _log.info("%s created.", self.name)
def __init__(self, host='localhost', port=8112, password='******'): """ Roughly the order of requests used in the built in deluge webui that we emulate 1. system.listMethods 2. auth.check_session: [] 3. web.register_event_listener [PluginDisabledEvent] 4. web.register_event_listener [PluginEnabledEvent] 5. web.get_events [] 6. web.connected [] 7. web.update_ui [["queue","name","total_size","state","progress","num_seeds", "total_seeds","num_peers","total_peers","download_payload_rate","upload_payload_rate", "eta","ratio","distributed_copies","is_auto_managed","time_added","tracker_host", "save_path","total_done","total_uploaded","max_download_speed","max_upload_speed", "seeds_peers_ratio"],{}] 8. web.get_plugins 9. web.update_ui 10. web.get_events [] (periodic?) If the password is not accepted _valid_password will be set to false and prevent any more requests from happening. :param host: Host of the deluge webui :type host: basestring :param port: Port of the webui (default: 8112) :type port: int :param password: Password to the webui (default: deluge) :type password: string """ super(DelugeClient, self).__init__() self._host = 'http://{}:{}'.format(host, port) self._endpoint = '{}/json'.format(self._host) self._session = requests.session() self._password = password self._headers = { 'Content-type': 'application/json', 'Accept': 'application/json' } self._req_id = 0 self._valid_password = True self._last_request = 0 self._host_up = True self._host_check = gevent.Greenlet(self._check_conn) self._host_check.start()
def start_command(self, cmd_id, *args, _callback=None, **kwargs): """ Start running a specific command by its command id """ assert isinstance(cmd_id, int) gevent.idle(constants.Priority.Normal.value) if cmd_id not in self._greenlets: self._greenlets[cmd_id] = gevent.Greenlet( self._commands[cmd_id].main, *args, **kwargs) green = self._greenlets[cmd_id] green.link( functools.partial( self._callback_wrapper, cmd_id, self._commands[cmd_id], _callback)) self._start(cmd_id)
def _try_load_service(services): """ this method tries to execute `validate` method on the services that failed to load when the robot started. Once all failed services are back to normal, this function will exit """ size = len(services) while size > 0: for service in services[:]: try: logger.debug("try to load %s again" % service.guid) service.validate() logger.debug("loading succeeded for %s" % service.guid) # validate passed, service is healthy again service.gl_mgr.add('executor', gevent.Greenlet(service._run)) services.remove(service) except: logger.debug("loading failed again for %s" % service.guid) gevent.sleep(10) # fixme: why 10 ? why not? size = len(services)
def _parallelize(self, functionname, clean=False, category=""): if functionname.__name__ in ['ping']: nodes = self._nids else: nodes = self._runningnids greens = list() for nid in nodes: greenlet = gevent.Greenlet(functionname, nid, clean) greenlet.nid = nid greenlet.start() greens.append(greenlet) gevent.joinall(greens) for green in greens: result = green.value if not result: results = [(green.nid, { 'message': str(green.exception), 'state': 'UNKNOWN' }, category)] self._returnResults(results)