def reset(self): """ Prepares jobs to be executed again """ for event in self.events.values(): event.clear() if self.progress > 0: self.progress = 0 self.state = self.STATE_RESET logging.debug("job {0} has been reset.".format(self.name))
def callwhen(event, fun): while True: try: event.wait() event.clear() fun() except gevent.GreenletExit: raise except Exception: traceback.print_exc() pass
def open(): # Open a new file with the given name. global next_id path = bottle.request.json['path'] data = {'id': next_id, 'path': path} if not os.path.exists(os.path.join(root, path)): bottle.response.status = 404 return {'status': 'no such file'} pending.append(data) next_id += 1 event.set() event.clear() return data
def schedule_loop(): heap = self._schedule event = self._schedule_event cur = gevent.getcurrent() now = time.time() while True: if heap: deadline = heap[0][0] timeout = min(5.0, max(0.0, deadline - now)) else: timeout = None if event.wait(timeout): event.clear() now = time.time() while heap and now >= heap[0][0]: _, callback = heapq.heappop(heap) greenlet = gevent.spawn(callback) cur.link(lambda glt: greenlet.kill())
def block_pusher(self): try: event = gevent.event.Event() while True: self._send_stratum_message(jsonrpc.create_request( 'mining.set_difficulty', [self.auth['difficulty']])) params = self.work.get_stratum_work(self.extranonce1) self._send_stratum_message(jsonrpc.create_request( 'mining.notify', params)) self.last_work_id = params[0] self.work.add_longpoll_event(event) event.wait() event.clear() except: logger.error("Pusher exception") logger.error(traceback.format_exc())
def main(): opt_parser = optparse.OptionParser(usage="%prog [options] REQ_PER_SEC " "SERVER_ADDR [SERVER_ADDR1...]") opt_parser.add_option('--profile', dest="is_profile", help="Turn on the profile", action="store_true", default=False) opt_parser.add_option('--cpu-affinity', dest="cpu_affinity", help="Set the CPU affinity", type=int) opt_parser.add_option('--runtime', dest="runtime", help="Run specified time and exit", type=int) opts, args = opt_parser.parse_args() if len(args) < 2: opt_parser.error('please enter REQ_PER_SEC SERVER_ADDR') req_num = args[0] try: req_num = int(req_num) except ValueError: opt_parser.error('please enter correct REQ_PER_SEC') server_addrs = args[1:] print 'connecting to %s' % str(server_addrs) logging.basicConfig(level=logging.DEBUG) if opts.cpu_affinity is not None: p = psutil.Process() p.set_cpu_affinity([opts.cpu_affinity]) with greenprofile.Profiler(opts.is_profile, 'client.profile'): client = recall.client.RpcClient() channel = client.get_tcp_channel(server_addrs) stub = test_pb2.TestService_Stub(channel) event = gevent.event.Event() def timer(): while True: event.set() gevent.sleep(1) gevent.spawn(timer) req = test_pb2.TestRequest(name='test_client', num=12345) sent_req_num = 0 recv_rsp_num = [0] def done(_, rsp): if rsp: recv_rsp_num[0] += 1 start_time = time.time() try: while True: event.wait() event.clear() print 'sent_req_num', sent_req_num, 'recv_rsp_num', recv_rsp_num[0] sent_req_num, recv_rsp_num[0] = 0, 0 for _ in xrange(req_num): controller = RpcController() stub.TestMethod(controller, req, done) sent_req_num = req_num if opts.runtime is not None: now = time.time() if now - start_time >= opts.runtime: break finally: client.close()
def wait(what, timeout=30): event = _get_or_make_event(what) LOG.debug('waiting on %s' % what) result = event.wait(timeout) event.clear() return result