def fork_happened2(): if uwsgi.i_am_the_spooler(): return print("worker %d is waiting for signal 100..." % uwsgi.worker_id()) uwsgi.signal_wait(100) print("worker %d received signal %d" % (uwsgi.worker_id(), uwsgi.signal_received())) print("fork() has been called [2] wid: %d" % uwsgi.worker_id())
def do_post_fork(): if UwsgiPostFork._verbose: UwsgiPostFork._uwsgi_broker.w_logger.debug("wid: {}, postfork hook called".format(uwsgi.worker_id())) UwsgiPostFork._uwsgi_broker.restful_comp().set_wid(uwsgi.worker_id()) msg_prefix = "wid: {}, ".format(uwsgi.worker_id()) uwsgi.atexit = UwsgiPostFork._uwsgi_broker.restful_comp()._on_exit if callable(UwsgiPostFork._postfork_op): UwsgiPostFork._postfork_op() else: if UwsgiPostFork._verbose: UwsgiPostFork._uwsgi_broker.w_logger.debug(msg_prefix + "'{}' is not defined by {}" .format(ComponentConstants.POST_FORK_CALLBACK_FUNC_NAME, UwsgiPostFork._uwsgi_broker.restful_comp())) if callable(UwsgiPostFork._configure_op): if UwsgiPostFork._verbose: UwsgiPostFork._uwsgi_broker.w_logger.debug(msg_prefix + "calling configure callback ...") UwsgiPostFork._configure_op() else: if UwsgiPostFork._verbose: UwsgiPostFork._uwsgi_broker.w_logger.debug(msg_prefix + "'{}' is not defined by {}" .format(ComponentConstants.CONFIGURE_CALLBACK_FUNC_NAME, UwsgiPostFork._uwsgi_broker.restful_comp())) if UwsgiPostFork._verbose: UwsgiPostFork._uwsgi_broker.w_logger.debug(msg_prefix + "calling model load callback ...") UwsgiPostFork._uwsgi_broker._reload_last_approved_model()
def uwsgi_entry_point(cls, restful_comp, pipeline_name, model_path, deputy_id, stats_path_filename, within_uwsgi_context, standalone): cls._wid = 0 if not within_uwsgi_context else uwsgi.worker_id() cls.w_logger = logging.getLogger("{}.{}".format( cls.__module__, cls.__name__)) cls.w_logger.info( "Entered to uWSGI entry point ... (wid: {}, pid: {}, ppid:{}". format(cls._wid, os.getpid(), os.getppid())) cls.w_logger.info( "Restful comp (wid: {}, pid: {}, ppid:{}): {}".format( cls._wid, os.getpid(), os.getppid(), restful_comp)) cls.w_logger.info("Standalone mode (wid: {}): {}".format( cls._wid, standalone)) restful_comp._ml_engine.set_standalone(standalone) cls._restful_comp = restful_comp cls._restful_comp._uuid_engine = deputy_id cls._restful_comp._stats_path_filename = stats_path_filename if within_uwsgi_context: UwsgiPostFork.init(cls) cls._setup_flask_app(pipeline_name, cls.w_logger) cls.register_model_load_handler(model_path, cls.w_logger, within_uwsgi_context) print("Deputy id {}".format(deputy_id)) print("Stats path {}".format(stats_path_filename)) # This should be the last printout in the uwsgi entry point function!!! print(WsgiMonitor.SUCCESS_RUN_INDICATION_MSG)
def a_post_fork_thread(): while True: time.sleep(3) if uwsgi.i_am_the_spooler(): print("Hello from a thread in the spooler") else: print("Hello from a thread in worker %d" % uwsgi.worker_id())
def index(): import uwsgi message = 'This is a notification from worker %d' % uwsgi.worker_id() db.session.execute(fawn.notify('ws', message)) db.session.commit() return """ <script> var s = new WebSocket("ws://" + location.host + "/ws/" + (Math.random() * 1000 << 1)); s.onopen = e => document.body.innerHTML += 'Socket opened' + '<br>' s.onmessage = e => document.body.innerHTML += e.data + '<br>' s.onerror = e => document.body.innerHTML += 'Error <br>' s.onclose = e => document.body.innerHTML += 'connection closed' + '<br>' </script> Page rendered on worker %d <br> """ % uwsgi.worker_id()
def __init__(self): self.last_msg = None self.arena = "arena{}".format(uwsgi.worker_id()) self.redis = redis.StrictRedis() self.channel = self.redis.pubsub() self.channel.subscribe(self.arena) self.redis_fd = self.channel.connection._sock.fileno()
def uwsgi_entry_point(cls, restful_comp, pipeline_name, model_path, within_uwsgi_context): cls._wid = 0 if not within_uwsgi_context else uwsgi.worker_id() cls.w_logger = logging.getLogger("{}.{}".format( cls.__module__, cls.__name__)) cls.w_logger.info( "Entered to uWSGI entry point ... (wid: {}, pid: {}, ppid:{}". format(cls._wid, os.getpid(), os.getppid())) cls.w_logger.info("Restful comp (wid:{}, pid: {}, ppid:{}): {}".format( cls._wid, os.getpid(), os.getppid(), restful_comp)) cls._restful_comp = restful_comp if within_uwsgi_context: UwsgiPostFork.init(cls) cls._setup_flask_app(pipeline_name, cls.w_logger) cls.register_model_load_handler(model_path, cls.w_logger, within_uwsgi_context) # This should be the last printout in the uwsgi entry point function!!! print(WsgiMonitor.SUCCESS_RUN_INDICATION_MSG)
def ensure_thread_mpc(): """Check if current worker has an MPC instance and create one. :return: per worker MPC instance """ global mpc_instances try: import uwsgi except ImportError: # If we are not in uwsgi mode, just return an MPC instance. if not mpc_instances: print_debug('NEW MPC INSTANCE') mpc_instances = MPC() return mpc_instances if mpc_instances is None: # Note: it is not necessary to use a dict here as the # mpc_instance variable will be unique per worker, but it looks nicer # this way. print_debug('NEW MPC DICT') mpc_instances = {} worker_id = uwsgi.worker_id() if worker_id not in mpc_instances: print_debug('NEW MPC INSTANCE FOR WORKER {}'.format(worker_id)) # See comment above about per worker dict. mpc_instances[worker_id] = MPC() # per worker MPC instance return mpc_instances[worker_id]
def print_with_color(msg: str, **kwargs): bold = kwargs.pop('bold', False) if bold: opts = kwargs.setdefault('opts', []) if 'bold' not in opts: opts.append('bold') pid = os.getpid() try: # noinspection PyPackageRequirements,PyUnresolvedReferences import uwsgi master = uwsgi.masterpid() worker = uwsgi.worker_id() mule = uwsgi.mule_id() except ImportError: uwsgi = None master = 0 worker = 0 mule = 0 if mule: print(colorize('[mule {}] {}'.format(mule, msg), **kwargs)) elif worker: print(colorize('[worker {}] {}'.format(worker, msg), **kwargs)) elif pid == master: print(colorize('[master] {}'.format(msg), **kwargs)) elif uwsgi: print(colorize('[spooler {}] {}'.format(pid, msg), **kwargs)) else: print(colorize(msg, **kwargs))
def uwsgi_context(request): try: # noinspection PyPackageRequirements import uwsgi return { 'UWSGI': { 'enabled': True, 'numproc': uwsgi.numproc, 'buffer_size': uwsgi.buffer_size, 'started_on': datetime.fromtimestamp(uwsgi.started_on, tz=utc), 'numworkers': len(uwsgi.workers()), 'masterpid': uwsgi.masterpid(), 'total_requests': uwsgi.total_requests(), 'request_id': uwsgi.request_id(), 'worker_id': uwsgi.worker_id(), } } except ImportError: return { 'UWSGI': { 'enabled': False, } }
def application(env, start_response): start_response('200 Ok', [('Content-Type', 'text/html')]) yield "I am the worker %d<br/>" % uwsgi.worker_id() grunt = uwsgi.grunt() if grunt is None: print("worker %d detached" % uwsgi.worker_id()) else: yield "And now i am the grunt with a fix worker id of %d<br/>" % uwsgi.worker_id() time.sleep(2) yield "Now, i will start a very slow task...<br/>" for i in range(1, 10): yield "waiting for %d seconds<br/>" % i time.sleep(i)
def instance_id(self): if not self._is_mule: instance_id = uwsgi.worker_id() elif self._farm_name: return self._mule_index_in_farm(self._farm_name) + 1 else: instance_id = uwsgi.mule_id() return instance_id
def spawn_consumers(): global q q = Queue.Queue() for i in range(CONSUMERS): t = Thread(target=consumer, args=(q, )) t.daemon = True t.start() print("consumer %d on worker %d started" % (i, uwsgi.worker_id()))
def application(env, start_response): start_response('200 Ok', [('Content-Type', 'text/html')]) yield "I am the worker %d<br/>" % uwsgi.worker_id() grunt = uwsgi.grunt() if grunt is None: print "worker %d detached" % uwsgi.worker_id() else: yield "And now i am the grunt with a fix worker id of %d<br/>" % uwsgi.worker_id() time.sleep(2) yield "Now, i will start a very slow task...<br/>" for i in xrange(1, 10): yield "waiting for %d seconds<br/>" % i time.sleep(i)
def spawn_consumers(): global q q = Queue.Queue() for i in range(CONSUMERS): t = Thread(target=consumer,args=(q,)) t.daemon = True t.start() print("consumer %d on worker %d started" % (i, uwsgi.worker_id()))
def filter(self, record): record.worker_id = 0 try: import uwsgi record.worker_id = uwsgi.worker_id() except ImportError: pass return True
def check_uwsgi(worker_callback=None, atexit=None): # type: (Optional[Callable], Optional[Callable]) -> None """Check whetever uwsgi is running and what needs to be done. :param worker_callback: Callback function to call in uWSGI worker processes. """ try: import uwsgi # type: ignore[import] except ImportError: return if not uwsgi.opt.get("enable-threads"): raise uWSGIConfigError("enable-threads option must be set to true") # If uwsgi has more than one process, it is running in prefork operational mode: uwsgi is going to fork multiple # sub-processes. # If lazy-app is enabled, then the app is loaded in each subprocess independently. This is fine. # If it's not enabled, then the app will be loaded in the master process, and uwsgi will `fork()` abruptly, # bypassing Python sanity checks. We need to handle this case properly. # The proper way to handle that is to allow to register a callback function to run in the subprocess at their # startup, and warn the caller that this is the master process and that (probably) nothing should be done. if uwsgi.numproc > 1 and not uwsgi.opt.get( "lazy-apps") and uwsgi.worker_id() == 0: if not uwsgi.opt.get("master"): # Having multiple workers without the master process is not supported: # the postfork hooks are not available, so there's no way to start a different profiler in each # worker raise uWSGIConfigError( "master option must be enabled when multiple processes are used" ) # Register the function to be called in child process at startup if worker_callback is not None: try: import uwsgidecorators # type: ignore[import] except ImportError: raise uWSGIConfigError( "Running under uwsgi but uwsgidecorators cannot be imported" ) uwsgidecorators.postfork(worker_callback) if atexit is not None: original_atexit = getattr(uwsgi, "atexit", None) def _atexit(): try: atexit() except Exception: pass if original_atexit is not None: original_atexit() uwsgi.atexit = _atexit raise uWSGIMasterProcess()
def application(environ, start_response): gevent.sleep() start_response('200 OK', [('Content-Type', 'text/html')]) yield "sleeping for 3 seconds...<br/>" gevent.sleep(3) yield "done<br/>" gevent.spawn(microtask, uwsgi.worker_id()) yield "microtask started<br/>"
def get_unique_id(): try: return uwsgi.worker_id() except Exception: try: return uwsgi.mule_id() except Exception: return os.getpid() return "unknown"
def uwsgi_friendly_setup(app): global WORKER_ID try: import uwsgi WORKER_ID = uwsgi.worker_id() except ImportError: # During development on local machine, we may use `flask run` command # in this case, uwsgi package won't be available and it will throw error pass
def get_apiserver(): worker_id = int(uwsgi.worker_id()) if worker_id > 0: vnc_api_server = api_server.VncApiServer( " -c /etc/contrail/contrail-api-%s.conf" " -c /etc/contrail/contrail-keystone-auth.conf" % (worker_id - 1)) pipe_start_app = vnc_api_server.get_pipe_start_app() if pipe_start_app is None: pipe_start_app = vnc_api_server.api_bottle return pipe_start_app
def uwsgi_info() -> Optional[dict]: try: import uwsgi return { 'numproc': uwsgi.numproc, 'worker_id': uwsgi.worker_id(), 'workers': uwsgi.workers() } except ImportError: # we are not under uWSGI return {'numproc': 0, 'worker_id': 'N/A', 'workers': []}
def increment_request_count(user_id): worker_id = str(uwsgi.worker_id()) if uwsgi.cache_get(worker_id): c = int(uwsgi.cache_get(worker_id)) c += 1 uwsgi.cache_update(worker_id, str(c)) else: uwsgi.cache_set(worker_id, '0') return f"user_id:{user_id}:workder_id:{worker_id}:request_number:{uwsgi.cache_get(worker_id).decode()}"
def ready(self): # Prevents the scheduler from running twice, django runs TWO instances of apps by default if os.environ.get('RUN_MAIN', None) != 'true': try: import uwsgi if uwsgi.worker_id() == 1: threading.Thread(target=scheduleConfig.schedulePinger, args=(), daemon=True).start() except ImportError: pass
def spawn_on_socket(fd): worker_id = uwsgi.worker_id() application = make_app(debug=options.debug) server = HTTPServer(application, xheaders=True, max_body_size=options.max_body_size) sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) server.add_sockets([sock]) if options.prometheus_port: prometheus_port = options.prometheus_port + worker_id uwsgi.log('starting prometheus server on port %d' % prometheus_port) start_http_server(prometheus_port) uwsgi.log('tornado plumber reporting for duty on uWSGI worker %s' % worker_id)
def __init__(self): ServiceBase.__init__(self) ProfileMixin.__init__(self) DispatcherMixin_CRR.__init__(self) if not uwsgi.cache_exists('Service2Counter'): uwsgi.cache_set('Service2Counter', '0') if not uwsgi.cache_exists('Service2Timer'): uwsgi.cache_set('Service2Timer', '0') print uwsgi.queue_size gevent.spawn(microtask, uwsgi.worker_id()) print 'after gevent.spawn'
def start_remote_debugger_if_correct_worker(*arg): with open(remote_pdb_filename) as f: wanted_worker = f.read().strip() this_worker = str(uwsgi.worker_id()) if wanted_worker != this_worker: print("This worker is {}, not {}".format(this_worker, wanted_worker)) return print("I'm worker {}! Starting remote PDB server on port {}.".format( this_worker, remote_pdb_port)) return RemotePdb('127.0.0.1', remote_pdb_port).set_trace()
def set_uwsgi_proc_name(): build_dir = os.path.basename(project_root) try: deploy_tag = open(os.path.join(project_root, '.DEPLOY_TAG')).read().strip() except IOError: deploy_tag = '?' os.environ['DEPLOY_TAG'] = deploy_tag uwsgi.setprocname("uwsgi worker %(worker_id)d <%(build)s> [%(tag)s]" % { 'worker_id': uwsgi.worker_id(), 'build': build_dir, 'tag': deploy_tag, })
def facts(self): facts = super(UWSGIApplicationStack, self).facts if not self._is_mule: facts.update({ 'pool_name': 'web', 'server_id': uwsgi.worker_id(), }) else: facts.update({ 'pool_name': self._farm_name, 'server_id': uwsgi.mule_id(), }) facts['instance_id'] = self.instance_id return facts
def init_log_handlers(): """init log handlers and print current configuration to log""" from everyclass.server.utils.logbook_logstash.handler import LogstashHandler from elasticapm.contrib.flask import ElasticAPM from everyclass.server.config import print_config ElasticAPM.request_finished = monkey_patch.ElasticAPM.request_finished( ElasticAPM.request_finished) global __app, __first_spawn # Elastic APM if __app.config['CONFIG_NAME'] in __app.config['APM_AVAILABLE_IN']: ElasticAPM(__app) print('APM is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # Logstash centralized log if __app.config['CONFIG_NAME'] in __app.config[ 'LOGSTASH_AVAILABLE_IN']: logstash_handler = LogstashHandler( host=__app.config['LOGSTASH']['HOST'], port=__app.config['LOGSTASH']['PORT'], release=__app.config['GIT_DESCRIBE'], bubble=True, logger=logger, filter=lambda r, h: r.level >= 11) # do not send DEBUG logger.handlers.append(logstash_handler) print( 'LogstashHandler is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # Sentry if __app.config['CONFIG_NAME'] in __app.config['SENTRY_AVAILABLE_IN']: sentry.init_app(app=__app) sentry_handler = SentryHandler(sentry.client, level='INFO') # Sentry 只处理 INFO 以上的 logger.handlers.append(sentry_handler) print('Sentry is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # print current configuration import uwsgi if uwsgi.worker_id() == 1 and __first_spawn: # set to warning level because we want to monitor restarts logger.warning('App (re)started in `{0}` environment'.format( __app.config['CONFIG_NAME']), stack=False) print_config(__app) __first_spawn = False
def init_log_handlers(): """初始化 log handlers 并将当前配置信息打 log""" from everyclass.server.utils.logbook_logstash.handler import LogstashHandler from elasticapm.contrib.flask import ElasticAPM from everyclass.server.config import print_config ElasticAPM.request_finished = monkey_patch.ElasticAPM.request_finished( ElasticAPM.request_finished) # Elastic APM if __app.config['CONFIG_NAME'] in __app.config['APM_AVAILABLE_IN']: ElasticAPM(__app) print('APM is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # Logstash centralized log if __app.config['CONFIG_NAME'] in __app.config[ 'LOGSTASH_AVAILABLE_IN']: logstash_handler = LogstashHandler( host=__app.config['LOGSTASH']['HOST'], port=__app.config['LOGSTASH']['PORT'], release=__app.config['GIT_DESCRIBE'], bubble=True, logger=logger, filter=lambda r, h: r.level >= 11) # do not send DEBUG logger.handlers.append(logstash_handler) print( 'LogstashHandler is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # Sentry if __app.config['CONFIG_NAME'] in __app.config['SENTRY_AVAILABLE_IN']: sentry.init_app(app=__app) sentry_handler = SentryHandler(sentry.client, level='INFO') # Sentry 只处理 INFO 以上的 logger.handlers.append(sentry_handler) print('Sentry is inited because you are in {} mode.'.format( __app.config['CONFIG_NAME'])) # 如果当前时间与模块加载时间相差一分钟之内,认为是第一次 spawn(进程随着时间的推移可能会被 uwsgi 回收), # 在 1 号 worker 里打印当前配置 import uwsgi if uwsgi.worker_id() == 1 and (datetime.datetime.now() - __load_time ) < datetime.timedelta(minutes=1): # 这里设置等级为 warning 因为我们希望在 sentry 里监控重启情况 logger.warning('App (re)started in `{0}` environment'.format( __app.config['CONFIG_NAME']), stack=False) print_config(__app)
def bootstrap(cls, mongo_db): """ Инициализация реестра справочников - подключение БД Mongo @param mongo_db: объект БД Mongo @type mongo_db: flask_pymongo.wrappers.Database | pymongo.database.Database @return: """ cls.db = mongo_db run = True try: import uwsgi run = uwsgi.worker_id() == 1 except ImportError: pass if run: cls.bootstrap_refbooks()
def init_log_handlers(): """init log handlers""" from everyclass.api_server.util.logbook_logstash.handler import LogstashHandler from elasticapm.contrib.flask import ElasticAPM global __app, __first_spawn, __sentry_available # Sentry if __app.config['CONFIG_NAME'] in __app.config['SENTRY_AVAILABLE_IN']: sentry.init_app(app=__app) sentry_handler = SentryHandler( sentry.client, level='WARNING') # Sentry 只处理 WARNING 以上的 logger.handlers.append(sentry_handler) __sentry_available = True logger.info('You are in {} mode, so Sentry is inited.'.format( __app.config['CONFIG_NAME'])) # Elastic APM if __app.config['CONFIG_NAME'] in __app.config['APM_AVAILABLE_IN']: ElasticAPM(__app) logger.info('You are in {} mode, so APM is inited.'.format( __app.config['CONFIG_NAME'])) # Logstash centralized log if __app.config['CONFIG_NAME'] in __app.config[ 'LOGSTASH_AVAILABLE_IN']: logstash_handler = LogstashHandler( host=__app.config['LOGSTASH']['HOST'], port=__app.config['LOGSTASH']['PORT'], release=__app.config['GIT_DESCRIBE'], bubble=True, logger=logger, filter=lambda r, h: r.level >= 11) # do not send DEBUG logger.handlers.append(logstash_handler) logger.info( 'You are in {} mode, so LogstashHandler is inited.'.format( __app.config['CONFIG_NAME'])) import uwsgi if uwsgi.worker_id() == 1 and __first_spawn: from everyclass.api_server.util import mysql_connect, get_semester_list logger.info('数据库可用学期:' + ';'.join(get_semester_list(__app.mongo_pool))) __first_spawn = False
def application(environ, start_response): global _application uwsgi.set_logvar('worker_id', str(uwsgi.worker_id())) if not os.environ.get('DJANGO_SETTINGS_MODULE'): os.environ['DJANGO_SETTINGS_MODULE'] = environ.get('DJANGO_SETTINGS_MODULE', 'settings') if _application is None: try: from django.core.wsgi import get_wsgi_application except ImportError: import django.core.handlers.wsgi _application = django.core.handlers.wsgi.WSGIHandler() else: _application = get_wsgi_application() return _application(environ, start_response)
def run(self): try: # If running in uwsgi, run AutoIndexer only in first worker! import uwsgi if uwsgi.worker_id() != 1: return except: pass try: while self.keep_running: self.check_path() if not self.interval: break time.sleep(self.interval) except KeyboardInterrupt: # pragma: no cover return
def index(): return """ <script> var sockets = []; function handle_socket(i) { var s = new WebSocket("ws://" + location.host + "/ws/" + i); s.onopen = e => document.body.innerHTML += i + ' opened <br>' s.onmessage = e => document.body.innerHTML += e.data + ' (' + i + ') <br>' s.onerror = e => document.body.innerHTML += 'Error (' + i + ')<br>' s.onclose = e => document.body.innerHTML += 'Socket closed (' + i + ')<br>' return s; } document.addEventListener('DOMContentLoaded', function () { for (var i = 0; i < 10; i++) { sockets.push(handle_socket(i)) } }); </script> Page rendered on worker %d <br> """ % uwsgi.worker_id()
def __init__(self, game, name, avatar, fd, x, y, r, color, speed=15): self.game = game self.name = name self.avatar = avatar self.fd = fd self.arena_object = ArenaObject(x, y, r, speed) self.attack = 0 self.energy = 100.0 self.arena = "arena{}".format(uwsgi.worker_id()) self.redis = redis.StrictRedis() self.channel = self.redis.pubsub() self.channel.subscribe(self.arena) self.redis_fd = self.channel.connection._sock.fileno() self.cmd = None self.attack_cmd = None self.bullet = Bullet(self.game, self) self.color = color
def init(): schema = Schema(title=TEXT(stored=True), path=TEXT(stored=True), content=TEXT) rel = dirname(__file__) index_name = 'index' if no_uwsgi: # attenzione: il restart provochera' la creazione di nuove cartelle # ogni volta... index_name += '{0}'.format(os.getpid()) else: index_name += '{0}'.format(uwsgi.worker_id()) index_dir = join(rel, index_name) if not exists(index_dir): mkdir(index_dir) logging.debug('create lyrics index') global ix ix = create_in(index_dir, schema) writer = ix.writer() lyrics_dir = join(rel, 'items') onlyfiles = [f for f in listdir(lyrics_dir) if isfile(join(lyrics_dir, f))] for file in onlyfiles: with open(join(lyrics_dir, file), encoding="utf-8") as f: cnt = f.read() link = cnt.splitlines()[0] writer.add_document( title=file, content=cnt, path=link) writer.commit()
def __init__(self, game, name, avatar, fd, x, y, z, r, color, scale=5, speed=15): self.size_x = 30 self.size_y = 35 self.size_z = 45 super(Player, self).__init__(game, 90.0, self.size_x, self.size_y, self.size_z, x, y, z, r) self.name = name self.avatar = avatar self.fd = fd self.last_msg = None self.scale = scale # self.attack = 0 self.energy = 100.0 self.arena = "arena{}".format(uwsgi.worker_id()) self.redis = redis.StrictRedis() self.channel = self.redis.pubsub() self.channel.subscribe(self.arena) self.redis_fd = self.channel.connection._sock.fileno() self.cmd = None # self.bullet = Bullet(self.game, self) self.color = color
def __init__(self, min_players=3, max_players=8, warmup=10): self.greenlets = { 'engine': self.engine_start, # 'start': self.start } self.posters = [ 'posters/robbo.jpg', 'posters/raffo.jpg', 'posters/unbit.jpg', 'posters/20tab.jpg', 'posters/beri.jpg', 'posters/pycon.jpg' ] self.animations = [] self.players = {} self.waiting_players = [] self.min_players = min_players self.max_players = max_players self.warmup = warmup self.started = False self.finished = False #self.warming_up = False self.walls = [] # self.ground_coordinates = (270, 1, 270, 0, 0, 0, 1) self.walls_coordinates = ( #sc_x, sc_y, sc_z, x, y, z, r (200, 100, 50, 0, 150, -1950, 0), (200, 100, 50, -1950, 150, 0, -math.pi / 2), (200, 100, 50, 1950, 150, 0, -math.pi / 2), (200, 100, 50, 0, 150, 1950, 0), ( 50, 50, 30, -730, 150, -1200, 0), ( 50, 50, 30, 730, 150, -1200, 0), ( 50, 50, 30, -1200, 150, -730, -math.pi / 2), ( 50, 50, 30, -1200, 150, 730, -math.pi / 2), ( 50, 50, 30, 1200, 150, -730, -math.pi / 2), ( 50, 50, 30, 1200, 150, 730, -math.pi / 2), ( 50, 50, 30, -730, 150, 1200, 0), ( 50, 50, 30, 730, 150, 1200, 0), ) self.spawn_points = ( # x, y, z, r, color #( 0, 1650, math.pi), #( 0, -1650, 0), ( -935, 35, 935, 3 * math.pi / 4, 0x7777AA), ( 935, 35, 935, 5 * math.pi / 4, 0x770000), ( 935, 35, -935, 7 * math.pi / 4, 0x007700), ( -935, 35, -935, math.pi / 4, 0x777700), (-1650, 35, 1650, 3 * math.pi / 4, 0xAA00AA), #(-1650, 0, math.pi / 2), #( 1650, 0, 3 * math.pi / 2), ( 1650, 35, 1650, 5 * math.pi / 4, 0x007777), ( 1650, 35, -1650, 7 * math.pi / 4, 0x000077), (-1650, 35, -1650, math.pi / 4, 0xFFAA77), ) self.broadphase = DbvtBroadphase() self.collisionConfiguration = DefaultCollisionConfiguration() self.dispatcher = CollisionDispatcher(self.collisionConfiguration) self.solver = SequentialImpulseConstraintSolver() self.world = DiscreteDynamicsWorld( self.dispatcher, self.broadphase, self.solver, self.collisionConfiguration) self.world.setGravity(Vector3(0, -9.81, 0)) self.ground_shape = StaticPlaneShape(Vector3(0, 1, 0), 1) q = Quaternion(0, 0, 0, 1) self.ground_motion_state = DefaultMotionState( Transform(q, Vector3(0, -1, 0))) construction_info = RigidBodyConstructionInfo( 0, self.ground_motion_state, self.ground_shape, Vector3(0, 0, 0)) construction_info.m_friction = 0.52 self.ground = RigidBody(construction_info) self.world.addRigidBody(self.ground) # self.ground = StaticBox(self.world, *self.ground_coordinates) for wall_c in self.walls_coordinates: wall = StaticBox(self.world, wall_c[0], wall_c[1], 6, wall_c[3], 0, wall_c[5], wall_c[6]) self.walls.append(wall) self.arena = "arena{}".format(uwsgi.worker_id()) self.redis = redis.StrictRedis() self.channel = self.redis.pubsub() self.channel.subscribe(self.arena) # self.bonus_malus = ( # BonusHaste, # # BonusGiant, # BonusPower, # BonusHeal, # ) self.bonus_malus_spawn_points = [ ( 0, 0), ( 0, 1650), ( 0, -1650), (-1650, 0), ( 1650, 0), ] self.active_bonus_malus = [] self.spawn_iterator = iter(self.spawn_points)
def __call__(self, *args, **kwargs): if self.f: if self.wid > 0 and self.wid != uwsgi.worker_id(): return return self.f() self.f = args[0]
def setprocname(): if uwsgi.worker_id() > 0: uwsgi.setprocname("i am the worker %d" % uwsgi.worker_id())
def application(env, start_response): try: uwsgi.mule_msg(env['REQUEST_URI'], 1) except: pass req = uwsgi.workers()[uwsgi.worker_id()-1]['requests'] uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req)) try: gc.collect(2) except: pass if DEBUG: print(env['wsgi.input'].fileno()) if env['PATH_INFO'] in routes: return routes[env['PATH_INFO']](env, start_response) if DEBUG: print(env['wsgi.input'].fileno()) try: gc.collect(2) except: pass if DEBUG: print(len(gc.get_objects())) workers = '' for w in uwsgi.workers(): apps = '<table border="1"><tr><th>id</th><th>mountpoint</th><th>startup time</th><th>requests</th></tr>' for app in w['apps']: apps += '<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>' % (app['id'], app['mountpoint'], app['startup_time'], app['requests']) apps += '</table>' workers += """ <tr> <td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td> </tr> """ % (w['id'], w['pid'], w['status'], w['running_time']/1000, w['avg_rt']/1000, w['tx'], apps) output = """ <img src="/logo"/> version %s running on %s (remote user: %s)<br/> <hr size="1"/> Configuration<br/> <iframe src="/config"></iframe><br/> <br/> Dynamic options<br/> <iframe src="/options"></iframe><br/> <br/> Workers and applications<br/> <table border="1"> <tr> <th>wid</th><th>pid</th><th>status</th><th>running time</th><th>average</th><th>tx</th><th>apps</th> </tr> %s </table> """ % (uwsgi.version, uwsgi.hostname, env.get('REMOTE_USER','None'), workers) start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(output)) )]) #return bytes(output.encode('latin1')) return output
def wait_for_signal(): if uwsgi.worker_id() != 2: print("waiting for a signal...") uwsgi.signal_wait() print("signal %d received" % uwsgi.signal_received())
def hello(signum): print("I AM THE WORKER %d" % uwsgi.worker_id())
def filter(self, record): record.worker_id = uwsgi.worker_id() record.mule_id = uwsgi.mule_id() return True
def notify(self, payload): import uwsgi self.send('Notification "%s" received in worker %d in ws %s' % ( payload, uwsgi.worker_id(), self.rand))
def tmp_modified(signum): print "/tmp has been touched, i am the greenlet %s running on worker %d" % (gevent.getcurrent(), uwsgi.worker_id()) if REFCNT: print sys.gettotalrefcount()
def notify_(self, payload): self.send( '"%s" recv (w%d) %s %d' % ( payload, uwsgi.worker_id(), request.path, g.rnd))
def notify(): message = '%%d (w%d)' % uwsgi.worker_id() for i in range(10): db.session.execute(fawn.notify('s%d' % i, message % i)) db.session.commit() return 'OK'
def __init__(self, min_players=3, max_players=8, warmup=10): self.greenlets = { 'engine': self.engine_start, 'start': self.start } self.posters = [ 'posters/robbo.jpg', 'posters/raffo.jpg', 'posters/unbit.jpg', 'posters/20tab.jpg', 'posters/beri.jpg', 'posters/pycon.jpg' ] self.debug = False self.bullets = [] self.players = {} self.waiting_players = [] self.min_players = min_players self.max_players = max_players self.warmup = warmup self.started = False self.finished = gevent.event.Event() self.walls = [] self.ramps = [] self.grounds = [] self.all_players = [] self.ground_coordinates = ( #sc_x, sc_y, sc_z, x, y, z, r (2000, 250, 2000, 0, -250, 0, 0), (2000, 431.5, 4000, 0, -69, -7350, 0), (6000, 1, 10000, 0, -500, -5500, 0) ) self.walls_coordinates = ( (150, 50, 50, 0, 215, -1950, 0), (200, 50, 50, -1950, 215, 0, -math.pi/2), (200, 50, 50, 1950, 215, 0, -math.pi/2), (200, 50, 50, 0, 215, 1950, 0), ( 50, 30, 30, -730, 125, -1200, 0), ( 50, 30, 30, 730, 125, -1200, 0), ( 50, 30, 30, -1200, 125, -730, -math.pi/2), ( 50, 30, 30, -1200, 125, 730, -math.pi/2), ( 50, 30, 30, 1200, 125, -730, -math.pi/2), ( 50, 30, 30, 1200, 125, 730, -math.pi/2), ( 50, 30, 30, -730, 125, 1200, 0), ( 50, 30, 30, 730, 125, 1200, 0), ) self.ramps_coordinates = ( (350, 10, 700, -1650, 172, -2679, math.pi/12), (350, 10, 700, 1650, 172, -2679, math.pi/12), (350, 10, 500, 0, 330, -7000, math.pi/12) ) self.spawn_points = ( # x, y, z, r, color #( 0, 1650, math.pi), #( 0, -1650, 0), ( -935, 35, 935, 3*math.pi/4, 0x7777AA), ( 935, 35, 935, 5*math.pi/4, 0x770000), ( 935, 35, -935, 7*math.pi/4, 0x007700), ( -935, 35, -935, math.pi/4, 0x777700), (-1650, 35, 1650, 3*math.pi/4, 0xAA00AA), #(-1650, 0, math.pi / 2), #( 1650, 0, 3 * math.pi / 2), ( 1650, 35, 1650, 5*math.pi/4, 0x007777), ( 1650, 35, -1650, 7*math.pi/4, 0x000077), (-1650, 35, -1650, math.pi/4, 0xFFAA77), ) self.broadphase = DbvtBroadphase() self.collisionConfiguration = DefaultCollisionConfiguration() self.dispatcher = CollisionDispatcher(self.collisionConfiguration) self.solver = SequentialImpulseConstraintSolver() self.world = DiscreteDynamicsWorld( self.dispatcher, self.broadphase, self.solver, self.collisionConfiguration) self.world.setGravity(Vector3(0, -10, 0)) for ground_c in self.ground_coordinates: ground = StaticBox(self.world, *ground_c) self.grounds.append(ground) #self.ground = StaticBox(self.world, *self.ground_coordinates) for wall_c in self.walls_coordinates: wall = StaticBox(self.world, 9.76, 4.37, 0.71, wall_c[3], wall_c[4], wall_c[5], wall_c[6], 0.5, wall_c[0], wall_c[1], wall_c[2]) self.walls.append(wall) for ramp_c in self.ramps_coordinates: ramp = StaticBox(self.world, *ramp_c, rot_vect=Vector3(1.0, 0.0, 0.0)) self.ramps.append(ramp) self.arena = "arena{}".format(uwsgi.worker_id()) self.redis = redis.StrictRedis() self.channel = self.redis.pubsub() self.channel.subscribe(self.arena) # self.bonus_malus = ( # BonusHaste, # # BonusGiant, # BonusPower, # BonusHeal, # ) self.bonus_malus_spawn_points = [ ( 0, 0), ( 0, 1650), ( 0, -1650), (-1650, 0), ( 1650, 0), ] self.active_bonus_malus = [] self.spawn_iterator = iter(self.spawn_points) self.sphere = ArenaSphere(self.world)
def postfork_setup(): from galaxy.app import app if process_is_uwsgi: import uwsgi app.config.server_name += ".%s" % uwsgi.worker_id() app.control_worker.bind_and_start()