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 increment_counter(): """ Increment by 1 the counter in mongoDB and fail seamlessly. """ try: app.mongo.counter.update( {'_id': 'counter'}, {'$inc': {'value': 1}}, upsert=True ) except: # since our main functions use the same code, we have to separate # the error handling between a frontend and a spooler process if uwsgi.i_am_the_spooler(): # failure means a mongoDB exception so let's try to reconnect get_mongo() # re-raise the exception so it's catched in the spooler function raise else: # failure means a mongoDB exception so let's try to reconnect gevent.spawn(get_mongo) # the frontend should spool a message to the spooler to ensure # that the increment will be retried and no data will be lost spooler.spool({'message': '1'}) finally: app.last_count += 1
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 __call__(self, *args, **kwargs): # ensure the spooler will not call it if uwsgi.i_am_the_spooler(): return uwsgi.lock() try: return self.f(*args, **kwargs) finally: uwsgi.unlock()
def decorated(*args, **kwargs): # ensure the spooler will not call it if i_am_the_spooler(self.lock_id): return lock(self.lock_id) try: return function(*args, **kwargs) finally: unlock(self.lock_id)
def increment_counter(): try: with api.app_context(): current_app.mongo.db.counter.update( {'_id': 'counter'}, {'$inc': {'value': 1}} ) except Exception: if uwsgi.i_am_the_spooler(): raise else: spooler.spool({'message': '1'})
def locked(*args, **kwargs): if uwsgi.i_am_the_spooler(): return if args[0].__is_locked: return f(*args, **kwargs) uwsgi.lock() args[0].__is_locked = True try: return f(*args, **kwargs) finally: args[0].__is_locked = False uwsgi.unlock()