Python gevent.getcurrent() Examples

The following are 20 code examples of gevent.getcurrent(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gevent , or try the search function .
Example #1
Source File: job.py    From mrq with MIT License 6 votes vote down vote up
def kill(self, block=False, reason="unknown"):
        """ Forcefully kill all greenlets associated with this job """

        current_greenletid = id(gevent.getcurrent())

        trace = "Job killed: %s" % reason
        for greenlet, job in context._GLOBAL_CONTEXT["greenlets"].values():
            greenletid = id(greenlet)
            if job and job.id == self.id and greenletid != current_greenletid:
                greenlet.kill(block=block)
                trace += "\n\n--- Greenlet %s ---\n" % greenletid
                trace += "".join(traceback.format_stack(greenlet.gr_frame))
            context._GLOBAL_CONTEXT["greenlets"].pop(greenletid, None)

        if reason == "timeout" and self.data["status"] != "timeout":
            updates = {
                "exceptiontype": "TimeoutInterrupt",
                "traceback": trace
            }
            self._save_status("timeout", updates=updates, exception=False) 
Example #2
Source File: context.py    From mrq with MIT License 5 votes vote down vote up
def set_current_job(job):
    current = gevent.getcurrent()

    current.__dict__["_trace_time"] = 0
    current.__dict__["_trace_switches"] = 0

    if job is None:
        if id(current) in _GLOBAL_CONTEXT["greenlets"]:
            del _GLOBAL_CONTEXT["greenlets"][id(current)]
    else:
        _GLOBAL_CONTEXT["greenlets"][id(current)] = (current, job) 
Example #3
Source File: loginserver.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def run(self):
        gevent.getcurrent().name = 'loginserver'
        self.logger.info('login server started')
        self.firewall.reset_firewall('blacklist')
        while True:
            for message in self.server_queue:
                handler = self.message_handlers[type(message)]
                try:
                    handler(message)
                except Exception as e:
                    if hasattr(message, 'peer'):
                        self.logger.error('an exception occurred while handling a message; passing it on to the peer...')
                        message.peer.disconnect(e)
                    else:
                        raise 
Example #4
Source File: authbot.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, config, incoming_queue):
        gevent.getcurrent().name = 'authbot'

        self.logger = logging.getLogger(__name__)
        self.incoming_queue = incoming_queue
        self.community_login_server = None
        self.hirez_login_server = None
        self.login_name = config['login_name']
        self.display_name = None
        self.password_hash = base64.b64decode(config['password_hash'])
        self.last_requests = {}

        self.smtp_server = config['smtp_server']
        self.smtp_port = int(config['smtp_port'])
        self.smtp_user = config['smtp_user']
        self.smtp_password = config['smtp_password']
        self.smtp_sender = config['smtp_sender']
        self.smtp_usetls = config.getboolean('smtp_usetls')

        self.message_handlers = {
            PeerConnectedMessage: self.handle_peer_connected,
            PeerDisconnectedMessage: self.handle_peer_disconnected,
            Login2AuthAuthCodeResultMessage: self.handle_authcode_result_message,
            Login2AuthChatMessage: self.handle_auth_channel_chat_message,
            LoginProtocolMessage: self.handle_login_protocol_message,
        }

        address_pair, errormsg = IPAddressPair.detect()
        if not address_pair.external_ip:
            raise NoPublicIpAddressError(errormsg)
        else:
            self.logger.info('authbot: detected external IP: %s' % address_pair.external_ip)
        self.login_server_address = address_pair.external_ip 
Example #5
Source File: geventwrapper.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def gevent_spawn_later(task_name: str, seconds, func, *args, **kwargs):

    def wrapper_func(*args, **kwargs):
        logger = logging.getLogger('gevent_spawn_later')
        gevent.getcurrent().name = task_name

        try:
            return func(*args, **kwargs)
        except Exception as e:
            logger.exception('%s greenlet terminated with an unhandled exception:' % task_name, exc_info=e)
            raise

    return gevent.spawn_later(seconds, wrapper_func, *args, **kwargs) 
Example #6
Source File: geventwrapper.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def gevent_spawn(task_name: str, func, *args, **kwargs):

    def wrapper_func(*args, **kwargs):
        logger = logging.getLogger('gevent_spawn')
        gevent.getcurrent().name = task_name

        try:
            return func(*args, **kwargs)
        except Exception as e:
            logger.exception('%s greenlet terminated with an unhandled exception:' % task_name, exc_info=e)
            raise

    return gevent.spawn(wrapper_func, *args, **kwargs) 
Example #7
Source File: pinghandler.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def handle_ping(ports):
    gevent.getcurrent().name = 'pinghandler'
    address = '0.0.0.0'
    port = ports['launcherping']
    try:
        EchoServer('%s:%d' % (address, port)).serve_forever()
    except OSError as e:
        if e.errno == 10048:
            raise PortInUseError('udp', address, port)
        else:
            raise 
Example #8
Source File: gameserverhandler.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, game_server_config, ports, server_handler_queue, launcher_queue):
        gevent.getcurrent().name = 'gameserver'

        self.servers = {}

        self.server_handler_queue = server_handler_queue
        self.launcher_queue = launcher_queue

        self.logger = logging.getLogger(__name__)
        self.ports = ports

        try:
            self.working_dir = game_server_config['dir']
            self.dll_to_inject = game_server_config['controller_dll']
            self.dll_config_path = game_server_config['controller_config']
        except KeyError as e:
            raise ConfigurationError("%s is a required configuration item under [gameserver]" % str(e))

        self.exe_path = os.path.join(self.working_dir, 'TribesAscend.exe')

        if not os.path.exists(self.working_dir):
            raise ConfigurationError(
                "Invalid 'dir' specified under [gameserver]: the directory does not exist")
        if not os.path.exists(self.exe_path):
            raise ConfigurationError(
                "Invalid 'dir' specified under [gameserver]: the specified directory does not contain a TribesAscend.exe")

        if not os.path.isabs(self.dll_to_inject):
            self.dll_to_inject = os.path.abspath(self.dll_to_inject)
        if not os.path.isabs(self.dll_config_path):
            self.dll_config_path = os.path.abspath(self.dll_config_path)
        self.logger.info(f'gameserver: path to controller DLL is {self.dll_to_inject}')
        self.logger.info(f'gameserver: path to controller configuration is {self.dll_config_path}')

        if not os.path.exists(self.dll_to_inject):
            raise ConfigurationError(
                "Invalid 'controller_dll' specified under [gameserver]: the specified file does not exist.")
        if not os.path.exists(self.dll_config_path):
            raise ConfigurationError(
                "Invalid 'controller_config' specified under [gameserver]: the specified file does not exist") 
Example #9
Source File: infra_logging.py    From RackHD with Apache License 2.0 5 votes vote down vote up
def filter(self, record):
        if record.pathname.startswith(self.__path_trim):
            record.pathname = record.pathname[len(self.__path_trim):]
        cur = gevent.getcurrent()
        if cur == self._main_greenlet:
            gname = 'gl-main'
        else:
            gname = getattr(cur, 'greenlet_name', str(cur))
        record.greenlet = '{0!s}'.format(gname)
        record.test_name = self.__current_test_name
        record.test_case_number = self.__current_test_case_number

        # This section removes the process, path, and greenlet information
        # from any mssage that starts with any of the characters ' +-'.
        # These are well known stream monitor messages and this informtion
        # is not required to be displayed.
        # TODO: there is probably a better way to do this but this will
        #       work until it is setup.
        if any(elem in record.msg[0] for elem in r' +-?%'):
            lif = ''
        else:
            u_lif = '{r.test_case_number} {r.process} {r.processName} {r.pathname}:{r.funcName}@{r.lineno} ' + \
                '{r.greenlet} [{r.test_name}]'
            lif = u_lif.format(r=record)
        record.location_info_string = lif

        return True 
Example #10
Source File: redis.py    From mrq with MIT License 5 votes vote down vote up
def run(self, params):

        def get_clients():
            return [c for c in connections.redis.client_list() if c.get("cmd") != "client"]

        def inner(i):
            print("Greenlet #%s, %s clients so far" % (id(gevent.getcurrent()), len(get_clients())))
            return connections.redis.get("test")

        if params["subpool_size"]:
          subpool_map(params["subpool_size"], inner, list(range(0, params["subpool_size"] * 5)))
        else:
          inner(0) 
Example #11
Source File: context.py    From mrq with MIT License 5 votes vote down vote up
def get_current_job(greenlet_id=None):
    if greenlet_id is None:
        greenlet_id = id(gevent.getcurrent())
    pair = _GLOBAL_CONTEXT["greenlets"].get(greenlet_id)
    if not pair:
        return None
    return pair[1] 
Example #12
Source File: gevent.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def _get_greenlet_scope(self, greenlet=None):
        if greenlet is None:
            greenlet = gevent.getcurrent()

        return getattr(greenlet, ACTIVE_ATTR, None) 
Example #13
Source File: base_consumer.py    From distributed_framework with Apache License 2.0 5 votes vote down vote up
def get_concurrent_info(cls):
        concurrent_info = ''
        if cls.global_concurrent_mode == 1:
            concurrent_info = f'[{threading.current_thread()}  {threading.active_count()}]'
        elif cls.global_concurrent_mode == 2:
            concurrent_info = f'[{gevent.getcurrent()}  {threading.active_count()}]'
        elif cls.global_concurrent_mode == 3:
            # noinspection PyArgumentList
            concurrent_info = f'[{eventlet.getcurrent()}  {threading.active_count()}]'
        return concurrent_info 
Example #14
Source File: basic01.py    From Python24 with MIT License 5 votes vote down vote up
def work5(num):

    for i in range(num):
        print("in work %s" % gevent.getcurrent())
        time.sleep(0.3) 
Example #15
Source File: greentest.py    From gsmtpd with MIT License 5 votes vote down vote up
def wrap_restore_handle_error(method):
    @wraps(method)
    def wrapped(self, *args, **kwargs):
        old = gevent.get_hub().handle_error
        try:
            return method(self, *args, **kwargs)
        finally:
            gevent.get_hub().handle_error = old
        if self.peek_error()[0] is not None:
            gevent.getcurrent().throw(*self.peek_error()[1:])
    return wrapped 
Example #16
Source File: gevent.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def _set_greenlet_scope(self, scope, greenlet=None):
        if greenlet is None:
            greenlet = gevent.getcurrent()

        setattr(greenlet, ACTIVE_ATTR, scope) 
Example #17
Source File: acehttp.py    From HTTPAceProxy with GNU General Public License v3.0 4 votes vote down vote up
def StreamReader(params):
    '''
    params: dict([url=] [file_index=] [infohash= ] [ad=1 [interruptable=1]] [stream=1] [pos=position] [bitrate=] [length=])
    '''

    def checkBroadcast():
        if not params['broadcastclients']:
           params['broadcast'].kill()
        return params['broadcast'].started

    def write_chunk(client, chunk, timeout=15.0):
        try: client.q.put(chunk, timeout=timeout)
        except gevent.queue.Full:
           client.send_error(500, '[%s]: does not read data until %s sec' % (client.clientip, timeout), logging.ERROR)

    def StreamWriter(url):
        for chunk in s.get(url, timeout=(5, AceConfig.videotimeout), stream=True).iter_content(chunk_size=1048576):
           _ = params['chunkpool'].map(lambda client: write_chunk(client, chunk), params['broadcastclients'])

    try:
       params.update({'url': urlparse(unquote(params['url']))._replace(netloc='{aceHostIP}:{aceHTTPport}'.format(**AceConfig.ace)).geturl(),
                      'broadcast': gevent.getcurrent(),
                      'broadcastclients': AceProxy.clientcounter.getClientsList(params['infohash']),
                      'chunkpool': Pool(),
                     })
       with requests.session() as s:
          if params['url'].endswith('.m3u8'): # AceEngine return link for HLS stream
             import m3u8; urls = []
             while checkBroadcast():
                for url in m3u8.load(params['url']).segments.uri:
                   if url in urls: continue
                   else:
                      StreamWriter(url)
                      urls.append(url)
                      if len(urls) > 50: urls.pop(0)

          else: StreamWriter(params['url']) #AceStream return link for HTTP stream
    except (TypeError, gevent.GreenletExit): pass
    except Exception as err:
       _ = AceProxy.pool.map(lambda x: x.send_error(500, repr(err), logging.ERROR), params['broadcastclients'])

# Spawning procedures 
Example #18
Source File: acehttp.py    From HTTPAceProxy with GNU General Public License v3.0 4 votes vote down vote up
def do_GET(self):
        '''
        GET request handler
        '''
        self.handlerGreenlet = gevent.getcurrent() # Current greenlet
        self.clientip = self.headers.get('X-Forwarded-For', self.address_string()) # Connected client IP address
        logging.info(unquote('[{clientip}]: {command} {request_version} request for: {path}'.format(**self.__dict__)))
        logging.debug('[%s]: Request headers: %s' % (self.clientip, dict(self.headers)))

        if AceConfig.firewall and not checkFirewall(self.clientip):
           self.send_error(401, '[{clientip}]: Dropping connection due to firewall rules'.format(**self.__dict__), logging.ERROR)

        self.path, _, self.query = self.path.partition('?')
        self.path = self.path.rstrip('/')
        # Pretend to work fine with Fake or HEAD request.
        if self.command == 'HEAD' or AceConfig.isFakeRequest(self.path, self.query, self.headers):
           # Return 200 and exit
           if self.command != 'HEAD': self.command = 'FAKE'
           logging.debug('[{clientip}]: {command} request: send headers and close the connection'.format(**self.__dict__))
           self.send_response(200)
           self.send_header('Content-Type', 'video/mp2t')
           self.send_header('Connection', 'Close')
           self.end_headers()
           return

        try:
           self.splittedpath = self.path.split('/')
           self.reqtype = self.splittedpath[1].lower()
           AceProxy.pluginshandlers[self.reqtype].handle(self) # If request should be handled by plugin
           raise IndexError()

        except (IndexError, KeyError):
           self.reqtype = {'torrent': 'url', 'pid': 'content_id'}.get(self.reqtype, self.reqtype) # For backward compatibility
           if self.reqtype in {'content_id', 'url', 'infohash', 'direct_url', 'data', 'efile_url'}:
              # Limit on the number of connected clients
              if 0 < AceConfig.maxconns <= len(AceProxy.clientcounter.getAllClientsList()):
                 self.send_error(403, "[{clientip}]: Maximum client connections reached, can't serve request".format(**self.__dict__), logging.ERROR)
              # Check if third path parameter is exists /{reqtype}/{reqtype_value}/.../.../video.mpg
              #                                                                           |_________|
              # And if it ends with regular video extension
              if not self.splittedpath[-1].endswith(('.avi', '.flv', '.m2ts', '.mkv', '.mpeg', '.mpeg4', '.mpegts',
                                                     '.mpg4', '.mp4', '.mpg', '.mov', '.mpv', '.qt', '.ts', '.wmv')):
                    self.send_error(501, '[{clientip}]: request seems like valid but no valid video extension was provided'.format(**self.__dict__), logging.ERROR)
              else:
                 self.handleRequest()
           elif self.reqtype not in AceProxy.pluginshandlers:
              self.send_error(400, '[{clientip}]: Bad Request'.format(**self.__dict__), logging.WARNING)  # 400 Bad Request

        except Exception as e:
           logging.error('Unexpected exception: %s' % repr(e))
           logging.error(traceback.format_exc()) 
Example #19
Source File: videoroom.py    From janus-cloud with GNU Affero General Public License v3.0 4 votes vote down vote up
def _idle_room_check_routine(self, server):
        idle_rooms = set()
        backend_handle = None
        while gevent.getcurrent() == self._idle_room_check_greenlets.get(server.name):
            gevent.sleep(self._idle_room_check_interval)
            if gevent.getcurrent() != self._idle_room_check_greenlets.get(server.name):
                break
            try:
                # create backend handle
                backend_handle = get_backend_session(
                    server.url,
                    auto_destroy=self._idle_room_check_interval * 2
                ).attach_handle(JANUS_VIDEOROOM_PACKAGE)

                reply_data, reply_jsep = _send_backend_message(backend_handle, {
                    'request': 'list'
                })
                room_list_info = reply_data.get('list', [])
                last_idle_rooms = idle_rooms
                idle_rooms = set()
                for room_info in room_list_info:
                    room_id = room_info.get('room', 0)

                    if room_id == 0 or room_info.get('num_participants', 1) > 0:
                        continue   # pass invalid or in-service room
                    if not room_info.get('description', '').startswith('januscloud'):
                        continue   # pass not januscloud-created room

                    # this is a idle room
                    if room_id in last_idle_rooms:
                        # auto destroy the idle room
                        log.warning('Sweeper found the backend idle room {} for server {}, destroy it'.format(
                            room_id, server.url))
                        backend_handle.send_message({
                            'request': 'destroy',
                            'room': room_id
                        })
                    else:
                        idle_rooms.add(room_id)  # destroy next time

            except Exception as e:
                # pass the exception
                log.debug('Videoroom backend sweeper failed on server {}: {}. Retry in {} secs'.format(
                    server.url, str(e), self._idle_room_check_interval))
            finally:
                if backend_handle:
                    backend_handle.detach()
                backend_handle = None 
Example #20
Source File: process_mngr.py    From janus-cloud with GNU Affero General Public License v3.0 4 votes vote down vote up
def _polling_run(watcher_weakref):
        current = gevent.getcurrent()

        while True:
            # print("check")
            watcher = watcher_weakref()
            if (watcher is None) or (not watcher.is_started()) \
                or (watcher._poll_greenlet != current):
                return     # make greenlet exit
            sleep_time = watcher._poll_interval
            try:
                if watcher._popen is None:
                    # restart
                    watcher.auto_restart_count += 1
                    watcher._launch_process()
                else:
                    # check the child process
                    ret = watcher._popen.poll()
                    if ret is not None:
                        # the process terminate
                        watcher._on_process_terminate(ret)
                        if watcher._error_restart_interval > 0:
                            if ret != 0:
                                # exit with error
                                sleep_time = watcher._error_restart_interval
                            else:
                                # exit normally
                                sleep_time = 0    # restart at once
                        else:
                            return   # if no need to restart, make the greenlet exit at once
                    else:
                        if watcher.age_time > 0:    # if age time present, check age
                            now = time.time()
                            if watcher._proc_start_time > now: # check time is changed
                                watcher._proc_start_time = now
                            if watcher._has_aged:
                                if now - watcher._proc_start_time > watcher.age_time + 5: # terminate no effect, kill it
                                    try:
                                        watcher._popen.kill()
                                    except OSError:
                                        pass
                            else:
                                if now - watcher._proc_start_time > watcher.age_time:
                                    watcher._has_aged = True
                                    try:
                                        watcher._popen.terminate()
                                    except OSError:
                                        pass
            except Exception as e:
                log.exception("process polling greenlet receives the below Exception when running, ignored")
                pass
            del watcher
            sleep(sleep_time)      # next time to check