def make_payments(bank): bank.pay_periods += 1 bank.last_pay_time = core.time() amt = bank.get_total_pending() Greenlet.spawn_later(bank.interval, globals()['make_payments']) if amt == 0: bank.log.debug('Payment period ended with no scheduled payments') return False svc = bank.get_proxy() to_pay = bank.pending bank.pending = {} bank.ips = set() bank.paid += [(core.time(), amt)] if bank.hist_file: fn = bank.hist_file if os.path.isfile(fn): os.rename(fn, fn+'.bak') with open(fn, 'wb') as pf: pickle.dump(bank.paid, pf) svc.sendmany(bank.acct, to_pay) vals = to_pay.values() bank.log.info('Made payments totalling %f to %d addresses', sum(vals), len(vals))
def deferToGreenletPool(reactor,pool, func, *args,**kwargs): """Call function using a greenlet from the given pool and return the result as a Deferred""" d = defer.Deferred() def task(): try: reactor.callFromGreenlet(d.callback,func(*args,**kwargs)) except: reactor.callFromGreenlet(d.errback,failure.Failure()) pool.add(Greenlet.spawn_later(0,task)) return d
def callInThreadWithCallback(self,onResult,func,*args,**kwargs): """Call a callable object in a separate greenlet and call onResult with the return value.""" if self.open: def task(*args, **kwargs): try: res = func(*args,**kwargs) except: onResult(False,failure.Failure()) else: onResult(True,res) self.add(Greenlet.spawn_later(0,task,*args,**kwargs))
def callInThreadWithCallback(self,onResult,func,*args,**kwargs): """Call a callable object in a separate greenlet and call onResult with the return value.""" if self.open: def task(): try: res = func(*args,**kwargs) except: onResult(False,failure.Failure()) else: onResult(True,res) self.add(Greenlet.spawn_later(0,task,*args,**kwargs))
def _schedule(self, mission): """ Set up gevent threads for each mission TODO: Replace gevent with spawn_process """ self.threads = [] for missionIndex in range(len(mission)): start_time = mission[missionIndex]['start_time'] if start_time: start_in = start_time - time.time() if (time.time() < start_time) else 0 print 'Start in ' + str(int(start_in)) + ' seconds' self.threads.append(Greenlet.spawn_later(start_in, self._run_mission, mission[missionIndex])) gevent.joinall(self.threads)
def deferToGreenletPool(*args, **kwargs): """Call function using a greenlet from the given pool and return the result as a Deferred""" reactor = args[0] pool = args[1] func = args[2] d = defer.Deferred() def task(): try: reactor.callFromGreenlet(d.callback, func(*args[3:], **kwargs)) except: reactor.callFromGreenlet(d.errback, failure.Failure()) pool.add(Greenlet.spawn_later(0, task)) return d
def callInGreenlet(self,*args,**kwargs): self.addToGreenletPool(Greenlet.spawn_later(0,*args,**kwargs))
def callInGreenlet(self, *args, **kwargs): self.addToGreenletPool(Greenlet.spawn_later(0, *args, **kwargs))
def callInThread(self, func, *args, **kwargs): """Call a callable object in a separate greenlet.""" if self.open: self.add(Greenlet.spawn_later(0, func, *args, **kwargs))
form = SensorForm(obj=sensor) if form.validate_on_submit(): sensor = sensor.update_from_form(form) flash('Updated sensor <u>%s</u>' % sensor.name, 'info') changed = True if changed: session.commit() session.close() return redirect(url_for('index')) return render_template('add_sensor.html', can_delete=True, label='Edit', form=form, callback=url_for('edit_sensor', alias=alias)) # // background thread functions def read_sensors(interval): print(interval) # // create background tasks for t in [Greenlet.spawn_later(10, read_sensors, interval=30)]: threads.append(t) if __name__ == '__main__': app.run(HOST, PORT, debug=DEBUG)
def callInThread(self,func,*args,**kwargs): """Call a callable object in a separate greenlet.""" if self.open: self.add(Greenlet.spawn_later(0,func,*args,**kwargs))
if __name__ == '__main__': app = bottle.default_app() try: conf_file = sys.argv[1] except IndexError: conf_file = 'sericata.conf' config = app.config.load_config(conf_file) url = 'http://%s:%s@%s:%s' % ( config['rpc.user'], config['rpc.pass'], config['rpc.host'], config['rpc.port'], ) config['rpc.url'] = url bank = CoinBank(config) config['sericata.bank'] = bank # GeventServer is more fragile than the default WSGIRefServer: extra # arguments must be filtered out of config gevent_conf = {'server': 'gevent'} for key, val in config.items(): if '.' not in key and key not in ('catchall','autojson'): gevent_conf[key] = val Greenlet.spawn_later(bank.interval, make_payments) app.run(**gevent_conf)