def bg_task(): for i in range(1,10): print "background task", i gevent.sleep(1) # task ended raise a signal !!! uwsgi.signal(17)
def sig(): try: uwsgi.signal(int(request.form['signum'])) flash("uwsgi signal sent") except: flash("unable to send signal") return redirect(url_for('index'))
def mess(): while True: for i in xrange(0, 100): if uwsgi.ready(): uwsgi.signal(17) print(i) time.sleep(0.1)
def bg_task(): for i in range(1, 10): print "background task", i gevent.sleep(1) # task ended raise a signal !!! uwsgi.signal(17)
def execute_async(self): if not uwsgi: return self.register_signal() uwsgi.signal(self.signal_id) self.free_signal()
def application(env, start_response): start_response('200 Ok', [('Content-Type', 'text/html')]) # this will send a signal to the master that will report it to the first available worker uwsgi.signal(30) uwsgi.signal(22) return "signals sent to workers"
def spoolerHandler(env): global counter # Spooler is handling a task with open(LOGFILE, "a") as log: print("%s" % (env['name']), file=log) counter += 1 if counter == len(tasks): # Each task has been processed. uwsgi.signal(17) # Spooler has done handling the task return uwsgi.SPOOL_OK
def _publish(self, data): """" Dispatch messages accross workers """ if self.has_workers: worker_id = self._cache_worker_id(data['room']) if self.worker_id == worker_id: logger.debug('Same worker (%s) relay msg internally' % worker_id) self._internal_emit(data) else: logger.debug('Other worker than me (%s) emit to worker %s' % (self.worker_id, worker_id)) self._cache_add_msg(worker_id, data) uwsgi.signal(worker_id) else: self._internal_emit(data)
def default_worker(signum): print "=" * 64 current_workers_tasks = getmemorystate() to_start_command = current_workers_tasks["to_start"] print "command %s" % (str(commands[to_start_command])) print "index %s" % (to_start_command) args = commands[to_start_command] try: call_command(*args) except: traceback.print_exc() print "=" * 64 print "Ooops the processes if blocked" print "=" * 64 # signal that telling that first worker that this worker is finishing uwsgi.signal(signum + signal_ending)
def application(e, s): s('200 OK', [('Content-Type', 'text/html')]) if e['PATH_INFO'] == '/30': uwsgi.signal(30) uwsgi.signal(100) else: uwsgi.signal(17) return "Signal raised"
def application(e, start_response): print e client = e['wsgi.input'].fileno() print client data = uwsgi.recv_block(client, 8) print "data", data, len(data) key1 = to_ws_num(e['HTTP_SEC_WEBSOCKET_KEY1']) key2 = to_ws_num(e['HTTP_SEC_WEBSOCKET_KEY2']) response = hashlib.md5( struct.pack('>II', key1, key2) + data).digest() print response start_response('101 WebSocket Protocol Handshake',[ ('Upgrade', 'WebSocket'), ('Connection', 'Upgrade'), ('Sec-WebSocket-Origin', e.get('HTTP_ORIGIN')), ('Sec-WebSocket-Location','ws://%s%s%s' % (e.get('HTTP_HOST'), e.get('SCRIPT_NAME'), e.get('PATH_INFO')) ), ('Sec-WebSocket-Protocol', e.get('HTTP_SEC_WEBSOCKET_PROTOCOL', 'default')) ]) yield response message = uwsgi.recv_frame(client, '\x00', '\xff') while message: print message uwsgi.signal(-17) yield '\x00' + message + '\xff' if len(message) == 0: raise StopIteration message = uwsgi.recv_frame(client, '\x00', '\xff')
def signal(self, name): uwsgi.signal(self.signals[name])
def raise_uwsgi_signal(): import uwsgi uwsgi.signal(66)
def an_infinite_task(args): for i in xrange(1, 4): print("infinite: %d %s" % (i, str(args))) print(uwsgi.call('helloworld')) uwsgi.signal(100) time.sleep(1)
def delayed_task(args): print("*** I am a delayed spool job. It is %s [%s]***" % (time.asctime(), str(args))) # send a signal to all workers uwsgi.signal(100)
def controlled_task(arguments): if arguments["arg"] != "alive" and "ghost" in arguments: print("We have a problem!") open(ghostpath, "w").close() uwsgi.signal(20)
def refresh_shared(*args): if settings.CONFIG_MODE = "LOCAL_FILE": return time.sleep(random.randrange(3, 8)) # jitter, working in spooler, so time.sleep is OK!!! airports = { "bjz" : "Beijing T3 internatianl airport", # Warning: fail to set because excced 20 bytes, but will not throw exc, get will be None "tjw" : "Tianjin T1 airport" } # backup current available to local file used to disaster recovery for e in uwsgi.cache_keys("airport"): AIRPORTS[e] = uwsgi.cache_get(e, "airport") with atomic_write(settings.CONFIG_FILE, overwrite=True) as f: f.write(json.dumps(AIRPORTS)) for k,v in airports.items(): uwsgi.cache_set(k, v, 0, "airport") # it's better to pack the `v` use struct to a fixed size string to fit the blocksize uwsgi.signal(17) # for i,k in enumerate(uwsgi.workers()): # avoid throughput jitter in one node # uwsgi.signal(17+i) def application(environ, start_response): if environ['PATH_INFO'].startswith('/favicon.ico'): start_response('404 Not Found', [('Content-Type', 'text/html')]) else: start_response('200 OK', [('Content-Type', 'text/html')]) if not AIRPORTS: yield "No airports" for k,v in AIRPORTS.items(): yield "code: %s: %s<br/>" % (k, v)
def update_embed(signal=None): if signal: util.download_embedding() uwsgi.signal(UPDATE_INDEX_SIGNAL)
def set_faiss_resources(signal=None): print('Getting Faiss resources') get_faiss_resources() if uwsgi and signal: uwsgi.signal(SIGNAL_SET_FAISS_INDEX)
def controlled_arguments_task(*args, **kwargs): if args != ({'key': 'value'}, 2) or kwargs != {'key1': 'value1'}: print("We have a problem!") open(ghostpath, 'w').close() uwsgi.signal(20)
def controlled_task(arguments): if arguments['arg'] != 'alive' and 'ghost' in arguments: print("We have a problem!") open(ghostpath, 'w').close() uwsgi.signal(20)
def reload(req): uwsgi.signal(98) return json_true(req, {})
def start(req): uwsgi.signal(97) return json_true(req, {})
import uwsgi # send a raw signal to register with file_monitor subsystem # uwsgi.signal(10, "/tmp/topolino") uwsgi.signal(10, "/tmp") # uwsgi.signal(10, "/root") # send a raw signal to register with timer subsystem uwsgi.signal(11, "3") uwsgi.signal(11, "4") uwsgi.signal(11, "8") def application(e, s): s('200 Ok', [('Content-Type', 'text/html')]) return "<h1>Hello World</h1>"
def stop(req): uwsgi.signal(99) return json_true(req, {})
def routing(signum): state = getmemorystate() current_workers_tasks = state["current_workers_tasks"] workers_tasks = state["workers_tasks"] # reload if state.has_key("stoping"): print "seems we are reload" print "let's start" if not len(current_workers_tasks.keys()): print "oo there is no tasks" print uwsgi.workers() del state["stoping"] setmemorystate(state) uwsgi.reload() return # stop if state.has_key("stop"): print "seems we are stoping" print "let's start" if not len(current_workers_tasks.keys()): print "oo there is not tasks" print uwsgi.workers() uwsgi.stop() return # working or not if state.has_key("working") and state["working"]: print "working" else: print "idle" return # choose command to start tmp_commands = commands.copy() for working_task in current_workers_tasks.keys(): print "delete %s " % current_workers_tasks[working_task]["comand_key"] del tmp_commands[current_workers_tasks[working_task] ["comand_key"]] # there is no possible exception here # if comman is existed in past half of length also do not start cmds = tmp_commands.keys() past_length = len(cmds) / 2 for past_work in workers_tasks[-1 * past_length:]: try: del tmp_commands[past_work] #but here it seems to be except KeyError: print "%s is already deleted from possible executin" % past_work cmds = tmp_commands.keys() to_start_command = None if len(cmds): to_start_command = random.choice(cmds) else: print "it's seems that everything is working recently, do not let workers to be lazy" to_start_command = random.choice(commands.keys()) workers_signal_tmp = workers_signal[:] for busy_worker in current_workers_tasks.keys(): print "delete from choice busy worker %i" % busy_worker workers_signal_tmp.remove(busy_worker) if len(workers_signal_tmp) > 0: print "choosing from the workers" print workers_signal_tmp number_of_signal = random.choice(workers_signal_tmp) workers_tasks.append(to_start_command) # write to shard memory index of command state["to_start"] = to_start_command current_workers_tasks[number_of_signal] = { "started": datetime.now(), "comand_key": to_start_command, "command": commands[to_start_command] } state["workers_tasks"] = workers_tasks[ -300:] # only 300 save in history state["current_workers_tasks"] = current_workers_tasks setmemorystate(state) try: print "ok sending signal %i" % number_of_signal print "and going start %s" % str(commands[to_start_command]) print "and going start %s" % to_start_command uwsgi.signal(number_of_signal) print workers_tasks except: traceback.print_exc() print "oh no %i busy" % number_of_signal else: print "=" * 64 print "there is no free workers ?!" print "=" * 64 print "busy workers " nw = datetime.now() for working_id in current_workers_tasks.keys(): print "%i -> %s" % (working_id, str(current_workers_tasks[working_id])) working_delta = nw - current_workers_tasks[working_id]["started"] if working_delta > dt(minutes=5): print "this process seems to be stuck" print "%i -> %s" % ( working_id, str(current_workers_tasks[working_id]["command"]))
def suspend(req): uwsgi.signal(96) return json_true(req, {})
def signal(self, num): ''' num : the signal number to raise ''' return uwsgi.signal(num)
import uwsgi # send a raw signal to register with file_monitor subsystem #uwsgi.signal(10, "/tmp/topolino") uwsgi.signal(10, "/tmp") #uwsgi.signal(10, "/root") # send a raw signal to register with timer subsystem uwsgi.signal(11, "3") uwsgi.signal(11, "4") uwsgi.signal(11, "8") def application(e, s): s('200 Ok', [('Content-Type', 'text/html')]) return "<h1>Hello World</h1>"
def controlled_raw_task(arguments): if arguments['arg'] != 'alive' and 'ghost' in arguments: print("We have a problem!") open(ghostpath, 'w').close() uwsgi.signal(20) return uwsgi.SPOOL_OK