我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncore.loop()。
def _cleanup(self): global _dispatcher_map self._shutdown = True if not self._thread: return log.debug("Waiting for event loop thread to join...") self._thread.join(timeout=1.0) if self._thread.is_alive(): log.warning( "Event loop thread could not be joined, so shutdown may not be clean. " "Please call Cluster.shutdown() to avoid this.") log.debug("Event loop thread was joined") # Ensure all connections are closed and in-flight requests cancelled for conn in tuple(_dispatcher_map.values()): conn.close() log.debug("Dispatchers were closed")
def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) self.deque = deque() self.deque_lock = Lock() self._connect_socket() asyncore.dispatcher.__init__(self, self._socket, _dispatcher_map) self._writable = True self._readable = True self._send_options_message() # start the event loop if needed self._loop.maybe_start()
def testchannel(self): i, a, c = self.setup(True) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def icond(hook, msg): if hook == 'admin' and msg['MsgType'] == 'Logon': sock.connect(('127.0.0.1', CHANNEL_PORT)) sock.send(json.dumps({'MsgType': 'NewOrderSingle'})) def acond(hook, msg): if hook == 'app' and msg['MsgType'] == 'NewOrderSingle': a.close() i.close() c.close() self.loop(i, a, icond, acond, False) self.assertEqual(json.loads(sock.recv(8192)), {'result': 'done'})
def line_terminator_check(self, term, server_chunk): event = threading.Event() s = echo_server(event) s.chunk_size = server_chunk s.start() event.wait() event.clear() time.sleep(0.01) # Give server time to start accepting. c = echo_client(term, s.port) c.push(b"hello ") c.push(b"world" + term) c.push(b"I'm not dead yet!" + term) c.push(SERVER_QUIT) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) s.join() self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) # the line terminator tests below check receiving variously-sized # chunks back from the server in order to exercise all branches of # async_chat.handle_read
def test_close_when_done(self): s, event = start_echo_server() s.start_resend_event = threading.Event() c = echo_client(b'\n', s.port) c.push(b"hello world\nI'm not dead yet!\n") c.push(SERVER_QUIT) c.close_when_done() asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) # Only allow the server to start echoing data back to the client after # the client has closed its connection. This prevents a race condition # where the server echoes all of its data before we can check that it # got any down below. s.start_resend_event.set() s.join() self.assertEqual(c.contents, []) # the server might have been able to send a byte or two back, but this # at least checks that it received something and didn't just fail # (which could still result in the client not having received anything) self.assertGreater(len(s.buffer), 0)
def line_terminator_check(self, term, server_chunk): event = threading.Event() s = echo_server(event) s.chunk_size = server_chunk s.start() event.wait() event.clear() time.sleep(0.01) # Give server time to start accepting. c = echo_client(term, s.port) c.push("hello ") c.push("world%s" % term) c.push("I'm not dead yet!%s" % term) c.push(SERVER_QUIT) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) s.join() self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"]) # the line terminator tests below check receiving variously-sized # chunks back from the server in order to exercise all branches of # async_chat.handle_read
def test_close_when_done(self): s, event = start_echo_server() s.start_resend_event = threading.Event() c = echo_client('\n', s.port) c.push("hello world\nI'm not dead yet!\n") c.push(SERVER_QUIT) c.close_when_done() asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) # Only allow the server to start echoing data back to the client after # the client has closed its connection. This prevents a race condition # where the server echoes all of its data before we can check that it # got any down below. s.start_resend_event.set() s.join() self.assertEqual(c.contents, []) # the server might have been able to send a byte or two back, but this # at least checks that it received something and didn't just fail # (which could still result in the client not having received anything) self.assertTrue(len(s.buffer) > 0)
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() self.addCleanup(t.join) for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): server = BaseServer(self.family, self.addr) t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() self.addCleanup(t.join) s = socket.socket(self.family, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
def serve_forever(self, poll_interval): """ Run the :mod:`asyncore` loop until normal termination conditions arise. :param poll_interval: The interval, in seconds, used in the underlying :func:`select` or :func:`poll` call by :func:`asyncore.loop`. """ try: asyncore.loop(poll_interval, map=self.sockmap) except select.error: # On FreeBSD 8, closing the server repeatably # raises this error. We swallow it if the # server has been closed. if self.connected or self.accepting: raise
def _fun_net_proc(self, async_ctx, req_queue): """ processing request queue :param async_ctx: :param req_queue: request queue :return: """ while True: if req_queue.empty() is False: try: ctl_flag, req_str = req_queue.get(timeout=0.001) if ctl_flag is False: break async_ctx.network_query(req_str) except Exception as e: traceback.print_exc() asyncore.loop(timeout=0.001, count=5)
def line_terminator_check(self, term, server_chunk): event = threading.Event() s = echo_server(event) s.chunk_size = server_chunk s.start() event.wait() event.clear() time.sleep(0.01) # Give server time to start accepting. c = echo_client(term, s.port) c.push(b"hello ") c.push(b"world" + term) c.push(b"I'm not dead yet!" + term) c.push(SERVER_QUIT) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) s.join(timeout=TIMEOUT) if s.is_alive(): self.fail("join() timed out") self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) # the line terminator tests below check receiving variously-sized # chunks back from the server in order to exercise all branches of # async_chat.handle_read
def test_close_when_done(self): s, event = start_echo_server() s.start_resend_event = threading.Event() c = echo_client(b'\n', s.port) c.push(b"hello world\nI'm not dead yet!\n") c.push(SERVER_QUIT) c.close_when_done() asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) # Only allow the server to start echoing data back to the client after # the client has closed its connection. This prevents a race condition # where the server echoes all of its data before we can check that it # got any down below. s.start_resend_event.set() s.join(timeout=TIMEOUT) if s.is_alive(): self.fail("join() timed out") self.assertEqual(c.contents, []) # the server might have been able to send a byte or two back, but this # at least checks that it received something and didn't just fail # (which could still result in the client not having received anything) self.assertGreater(len(s.buffer), 0)
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): server = BaseServer(self.family, self.addr) t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() def cleanup(): t.join(timeout=TIMEOUT) if t.is_alive(): self.fail("join() timed out") self.addCleanup(cleanup) s = socket.socket(self.family, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except OSError: pass finally: s.close()
def serve_forever(self, poll_interval): """ Run the :mod:`asyncore` loop until normal termination conditions arise. :param poll_interval: The interval, in seconds, used in the underlying :func:`select` or :func:`poll` call by :func:`asyncore.loop`. """ try: asyncore.loop(poll_interval, map=self._map) except OSError: # On FreeBSD 8, closing the server repeatably # raises this error. We swallow it if the # server has been closed. if self.connected or self.accepting: raise
def _cleanup(loop_weakref): try: loop = loop_weakref() except ReferenceError: return loop._cleanup()
def validate(self): assert not self._notified self.notify_loop() assert self._notified self.loop(0.1) assert not self._notified
def loop(self, timeout): asyncore.loop(timeout=timeout, use_poll=True, map=_dispatcher_map, count=1)
def loop(self, timeout): asyncore.loop(timeout=timeout, use_poll=False, map=_dispatcher_map, count=1)
def loop(self, timeout): if not _dispatcher_map: time.sleep(0.005) count = timeout // self.max_write_latency asyncore.loop(timeout=self.max_write_latency, use_poll=True, map=_dispatcher_map, count=count)
def _run_loop(self): log.debug("Starting asyncore event loop") with self._loop_lock: while not self._shutdown: try: self._loop_dispatcher.loop(self.timer_resolution) self._timers.service_timeouts() except Exception: log.debug("Asyncore event loop stopped unexepectedly", exc_info=True) break self._started = False log.debug("Asyncore event loop ended")
def run( self ): while not self._stopevent.isSet(): asyncore.loop( timeout = SmtpMailsink.TIME_TO_WAIT_BETWEEN_CHECKS_TO_STOP_SERVING, count = 1 )
def start(self): self.server = smtpd_mock.SMTPServerMock(('0.0.0.0', 4444), None) asyncore.loop()
def run(self): asyncore.loop(use_poll=True,timeout=1)
def stop(self): #we dont use server.close() as this raises a bad file decritoor exception in loop self.server.connected = False self.server.accepting = False self.server.del_channel() self.join() self.server.socket.close() logger.debug("Server Thread closed")
def main(): wm = pyinotify.WatchManager() # watched events mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE notifier = pyinotify.AsyncNotifier(wm, EventHandler()) wdd = wm.add_watch(SAMPLES_DIR, mask, rec=True) asyncore.loop()
def loop(self, *args, **kwargs): if "discrete" in kwargs: discreteVal = kwargs["discrete"] del kwargs["discrete"] while True: asyncore.loop(*args, **kwargs) time.sleep(discreteVal) try: callback = self.__class__.__detachedQueue.get(False) #doesn't block callback() except Queue.Empty: pass else: asyncore.loop(*args, **kwargs)
def pop_all (self): # DNS query maybe not allowed delay between request and send # maybe they just drop response packet for delaying with self.lock: queue, self.queue = self.queue [:], [] count = len (queue) while queue: name, args = queue.pop (0) self.handler.handle_request (name, **args) if (not count and not self.has_job ()): return map = {} with self.lock: for client in self.udps: map [client._fileno] = client fds = list (map.keys ()) # maybe 2 is enough safeguard = count * 2 while self.has_job () and safeguard: safeguard -= 1 asyncore.loop (0.1, map, count = 1) if safeguard % 5 == 0: self.maintern (time.time ()) self.maintern (time.time ()) for fd in fds: if fd not in map: # resync try: del asyncore.socket_map [fd] except KeyError: pass
def _add (method, url, params = None, auth = None, headers = {}, callback = None, meta = None, proxy = None): global _que, _initialized, _dns_query_req, _dns_reqs, _workers if not _initialized: configure () if not meta: meta = {} meta ['req_id'] = _que.req_id meta ['req_method'] = method meta ['req_callback'] = callback _que.add ((method, url, params, auth, headers, meta, proxy)) # DNS query for caching and massive if not lifetime._polling: host = urlparse (url) [1].split (":")[0] if _dns_reqs < _workers and host not in _dns_query_req: _dns_query_req [host] = None _dns_reqs += 1 adns.query (host, "A", callback = lambda x: None) asyndns.pop_all () asyncore.loop (0.1, count = 2) #print ('~~~~~~~~~~~~~~~', asyndns.pool.connections) #---------------------------------------------------- # Add Reuqest (protocols.*.request) Object #----------------------------------------------------
def connect(self): self.host = self.root.ids.server.text self.nick = self.root.ids.nickname.text self.client = MySocketClient((self.host, PORT), self) threading.Thread(target=asyncore.loop).start() print('-- connecting to ' + self.host) self.root.current = 'chatroom'
def loop(self, i, a, icond=None, acond=None, reset=False): if icond is not None: i.register('app', icond) i.register('admin', icond) if acond is not None: a.register('app', acond) a.register('admin', acond) i.logon(5, None, reset) asyncore.loop()
def testlogon(self): i, a = self.setup() def cond(hook, msg): if hook == 'admin' and msg['MsgType'] == 'Logon': self.assertEquals(msg['SenderCompID'], 'B') self.assertEquals(msg['TargetCompID'], 'A') a.close() i.close() self.loop(i, a, cond, None)
def testreset(self): i, a = self.setup() def cond(hook, msg): if hook == 'admin' and msg['MsgType'] == 'Logon': self.assertEquals(msg.get('ResetSeqNumFlag'), True) self.assertEquals(msg['MsgSeqNum'], 1) a.close() i.close() self.loop(i, a, cond, None, True)
def enable_asyncore_loop(self): """Hooks our asyncore loop into Qt's event queue.""" def beat(): asyncore.loop(count=1, timeout=0) # Yep, this isn't especially real-time IO, but it's fine for what we do. timer = QTimer() timer.timeout.connect(beat) timer.setSingleShot(False) timer.setInterval(15) timer.start() self._timer = timer
def disable_asyncore_loop(self): """Removes our asyncore loop from Qt's event queue.""" self._timer = None
def __init__(self, host, port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.address = (host, port) self.bind(self.address) self.listen(1) self.remote_clients = {} self.map = RaceMap(self) self.network_loop = threading.Thread(target = (lambda: asyncore.loop(timeout = 3))) self.packet_types = {'L': self.ServerPacketLogin, 'C': self.ServerPacketControl, 'E': self.ServerPacketLogout}
def tick(self, t): """ Game loop """ if len(self.player_data) > 0: self.game_started = True # tick player self.tick_player(t) elif self.game_started: self.running = False self.last_tick = t
def run(self): while not self._stop_event.isSet(): asyncore.loop(timeout=0.01, count=1)