本文整理汇总了Python中minitwisted.ThreadedReactor.stop方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadedReactor.stop方法的具体用法?Python ThreadedReactor.stop怎么用?Python ThreadedReactor.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类minitwisted.ThreadedReactor
的用法示例。
在下文中一共展示了ThreadedReactor.stop方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _main_loop
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class TestSend:
def _main_loop(self):
return time.time() + 100, [DATAGRAM1]
def _callback(self, value):
with self.lock:
self.callback_values.append(value)
return time.time() + 100, [DATAGRAM2]
def _on_datagram_received(self, datagram):
with self.lock:
self.datagrams_received.append(datagram)
return time.time() + 100, [DATAGRAM3]
def _crashing_callback(self):
raise Exception, "Crash testing"
def setup(self):
self.main_loop_call_counter = 0
self.callback_values = []
self.datagrams_received = []
self.lock = threading.RLock()
self.reactor = ThreadedReactor(
self._main_loop, tc.CLIENT_PORT, self._on_datagram_received, task_interval=tc.TASK_INTERVAL
)
self.reactor.s = _SocketMock()
self.s = self.reactor.s
self.reactor.start()
def test_main_loop_send_data(self):
time.sleep(tc.TASK_INTERVAL)
eq_(self.s.get_datagrams_sent(), [DATAGRAM1])
return
def test_call_asap_send_data(self):
time.sleep(tc.TASK_INTERVAL)
eq_(self.s.get_datagrams_sent(), [DATAGRAM1])
self.reactor.call_asap(self._callback, 1)
time.sleep(tc.TASK_INTERVAL * 2)
eq_(self.s.get_datagrams_sent(), [DATAGRAM1, DATAGRAM2])
def test_on_datagram_received_send_data(self):
time.sleep(tc.TASK_INTERVAL)
eq_(self.s.get_datagrams_sent(), [DATAGRAM1])
self.s.put_datagram_received(Datagram(DATA1, tc.SERVER_ADDR))
time.sleep(tc.TASK_INTERVAL / 2)
eq_(self.s.get_datagrams_sent(), [DATAGRAM1, DATAGRAM3])
def teardown(self):
self.reactor.stop()
示例2: __init__
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class Controller:
def __init__(self, dht_addr):
my_addr = dht_addr
my_id = identifier.RandomId()
my_node = Node(my_addr, my_id)
tracker_ = tracker.Tracker()
token_m = token_manager.TokenManager()
self.reactor = ThreadedReactor()
rpc_m = RPCManager(self.reactor, my_addr[1])
querier_ = Querier(rpc_m, my_id)
routing_m = RoutingManager(my_node, querier_,
bootstrap_nodes)
responder_ = Responder(my_id, routing_m,
tracker_, token_m)
responder_.set_on_query_received_callback(
routing_m.on_query_received)
querier_.set_on_response_received_callback(
routing_m.on_response_received)
querier_.set_on_error_received_callback(
routing_m.on_error_received)
querier_.set_on_timeout_callback(routing_m.on_timeout)
querier_.set_on_nodes_found_callback(routing_m.on_nodes_found)
routing_m.do_bootstrap()
rpc_m.add_msg_callback(QUERY,
responder_.on_query_received)
self.lookup_m = LookupManager(my_id, querier_,
routing_m)
self._routing_m = routing_m
def start(self):
self.reactor.start()
def stop(self):
#TODO2: stop each manager
self.reactor.stop()
def get_peers(self, info_hash, callback_f, bt_port=None):
return self.lookup_m.get_peers(info_hash, callback_f, bt_port)
def print_routing_table_stats(self):
self._routing_m.print_stats()
示例3: __init__
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class Controller:
def __init__(self, dht_addr):
self.my_addr = dht_addr
self.my_id = identifier.RandomId()
self.my_node = Node(self.my_addr, self.my_id)
self.tracker = tracker.Tracker()
self.token_m = token_manager.TokenManager()
self.reactor = ThreadedReactor()
self.rpc_m = RPCManager(self.reactor, self.my_addr[1])
self.querier = Querier(self.rpc_m, self.my_id)
self.routing_m = RoutingManager(self.my_node, self.querier,
bootstrap_nodes)
self.responder = Responder(self.my_id, self.routing_m,
self.tracker, self.token_m)
self.responder.set_on_query_received_callback(
self.routing_m.on_query_received)
self.querier.set_on_response_received_callback(
self.routing_m.on_response_received)
self.querier.set_on_error_received_callback(
self.routing_m.on_error_received)
self.querier.set_on_timeout_callback(self.routing_m.on_timeout)
self.querier.set_on_nodes_found_callback(self.routing_m.on_nodes_found)
self.routing_m.do_bootstrap()
self.rpc_m.add_msg_callback(QUERY,
self.responder.on_query_received)
self.lookup_m = LookupManager(self.my_id, self.querier,
self.routing_m)
def start(self):
self.reactor.start()
def stop(self):
#TODO2: stop each manager
self.reactor.stop()
def get_peers(self, info_hash, callback_f, bt_port=None):
return self.lookup_m.get_peers(info_hash, callback_f, bt_port)
示例4: test_listen_upd
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
def test_listen_upd(self):
r = ThreadedReactor()
r.start()
logger.warning(''.join(
('TESTING LOGS ** IGNORE EXPECTED WARNING ** ',
'(udp_listen has not been called)')))
self.client_r.sendto(DATA, tc.SERVER_ADDR)
while 1: #waiting for data
with self.lock:
if self.datagrams_received:
break
time.sleep(tc.TASK_INTERVAL)
with self.lock:
first_datagram = self.datagrams_received.pop(0)
logger.debug('first_datagram: %s, %s' % (
first_datagram,
(DATA, tc.CLIENT_ADDR)))
assert first_datagram, (DATA, tc.CLIENT_ADDR)
r.stop()
示例5: test_mock_threaded_reactor
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
def test_mock_threaded_reactor(self):
'''
Just making sure that the interface is the same
'''
r = ThreadedReactor(task_interval=.1)
rm = ThreadedReactorMock(task_interval=.1)
r.listen_udp(tc.CLIENT_ADDR[1], lambda x,y:None)
rm.listen_udp(tc.CLIENT_ADDR[1], lambda x,y:None)
r.start()
rm.start()
r.sendto(DATA, tc.CLIENT_ADDR)
rm.sendto(DATA, tc.CLIENT_ADDR)
r.call_later(.1, self._callback)
rm.call_later(.1, self._callback)
# time.sleep(.002)
r.stop()
rm.stop()
示例6: _main_loop
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class TestMinitwistedRealThreading:
def _main_loop(self):
return time.time() + 1, []
def _on_datagram_received(self, datagram):
return time.time() + 1, []
def test_start_and_stop(self):
'''
NOTE:
This is the only test using real threading
'''
self.reactor = ThreadedReactor(self._main_loop,
tc.CLIENT_PORT,
self._on_datagram_received,
task_interval=tc.TASK_INTERVAL)
ok_(not self.reactor.running)
self.reactor.start()
time.sleep(.1)
ok_(self.reactor.running)
self.reactor.stop()
ok_(not self.reactor.running)
示例7: __init__
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class Controller:
def __init__(self, dht_addr, state_path,
routing_m_mod, lookup_m_mod,
private_dht_name):
#TODO: don't do this evil stuff!!!
message.private_dht_name = private_dht_name
self.state_filename = os.path.join(state_path, STATE_FILENAME)
self.load_state()
if not self._my_id:
self._my_id = identifier.RandomId()
self._my_node = Node(dht_addr, self._my_id)
self._tracker = tracker.Tracker()
self._token_m = token_manager.TokenManager()
self._reactor = ThreadedReactor()
self._reactor.listen_udp(self._my_node.addr[1],
self._on_datagram_received)
#self._rpc_m = RPCManager(self._reactor)
self._querier = Querier(self._my_id)
bootstrap_nodes = self.loaded_nodes or BOOTSTRAP_NODES
del self.loaded_nodes
self._routing_m = routing_m_mod.RoutingManager(self._my_node,
bootstrap_nodes)
# self._responder = Responder(self._my_id, self._routing_m,
# self._tracker, self._token_m)
self._lookup_m = lookup_m_mod.LookupManager(self._my_id)
current_time = time.time()
self._next_maintenance_ts = current_time
self._next_save_state_ts = current_time + SAVE_STATE_DELAY
self._running = False
def start(self):
assert not self._running
self._running = True
self._reactor.start()
self._main_loop()
def stop(self):
assert self._running
#TODO2: stop each manager
self._reactor.stop()
def save_state(self):
rnodes = self._routing_m.get_main_rnodes()
f = open(self.state_filename, 'w')
f.write('%r\n' % self._my_id)
for rnode in rnodes:
f.write('%d\t%r\t%s\t%d\t%f\n' % (
self._my_id.log_distance(rnode.id),
rnode.id, rnode.addr[0], rnode.addr[1],
rnode.rtt * 1000))
f.close()
def load_state(self):
self._my_id = None
self.loaded_nodes = []
try:
f = open(self.state_filename)
except(IOError):
return
# the first line contains this node's identifier
hex_id = f.readline().strip()
self._my_id = Id(hex_id)
# the rest of the lines contain routing table nodes
# FORMAT
# log_distance hex_id ip port rtt
for line in f:
_, hex_id, ip, port, _ = line.split()
addr = (ip, int(port))
node_ = Node(addr, Id(hex_id))
self.loaded_nodes.append(node_)
f.close
def get_peers(self, lookup_id, info_hash, callback_f, bt_port=0):
assert self._running
# look if I'm tracking this info_hash
local_peers = self._tracker.get(info_hash)
# do the lookup
log_distance = info_hash.log_distance(self._my_id)
bootstrap_rnodes = self._routing_m.get_closest_rnodes(log_distance,
None,
True)
lookup_obj = self._lookup_m.get_peers(lookup_id, info_hash,
callback_f, bt_port)
lookup_queries_to_send = lookup_obj.start(bootstrap_rnodes)
self._send_queries(lookup_queries_to_send)
if not lookup_queries_to_send:
# There are no nodes in my routing table, announce to myself
self._announce(lookup_obj)
# NOTICE: the callback is NOT triggered, zero is returned.
return len(lookup_queries_to_send), local_peers
def print_routing_table_stats(self):
self._routing_m.print_stats()
#.........这里部分代码省略.........
示例8: _main_loop
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class TestMinitwisted:
def _main_loop(self):
print 'main loop call'
self.main_loop_call_counter += 1
return time.time() + self.main_loop_delay, []
def _main_loop_return_datagrams(self):
return time.time() + self.main_loop_delay, [DATAGRAM1]
def _callback(self, value):
self.callback_values.append(value)
return []
def _very_long_callback(self, value):
time.sleep(tc.TASK_INTERVAL*11)
return []
def _on_datagram_received(self, datagram):
print 'on_datagram', datagram, datagram.data, datagram.addr
self.datagrams_received.append(datagram)
return time.time() + 100, []
def _crashing_callback(self):
raise CrashError, 'Crash testing'
def setup(self):
time.mock_mode()
self.main_loop_call_counter = 0
self.callback_values = []
self.datagrams_received = []
self.main_loop_delay = MAIN_LOOP_DELAY
self.reactor = ThreadedReactor(self._main_loop,
tc.CLIENT_PORT,
self._on_datagram_received,
task_interval=tc.TASK_INTERVAL)
self.reactor.s = _SocketMock()
#self.reactor.start() >> instead of usint start(), we use run_one_step()
def test_start_and_stop(self):
'''
NOTE:
This is the only test using real threading
'''
ok_(not self.reactor.running)
self.reactor.start()
ok_(self.reactor.running)
self.reactor.stop()
ok_(not self.reactor.running)
def test_call_main_loop(self):
eq_(self.main_loop_call_counter, 0)
self.reactor.run_one_step()
# main_loop is called right away
eq_(self.main_loop_call_counter, 1)
self.reactor.run_one_step()
# no events
eq_(self.main_loop_call_counter, 1)
time.sleep(self.main_loop_delay)
self.reactor.run_one_step()
# main_loop is called again after
eq_(self.main_loop_call_counter, 2)
def test_call_asap(self):
eq_(self.callback_values, [])
self.reactor.call_asap(self._callback, 0)
eq_(self.callback_values, []) # stil nothing
self.reactor.run_one_step()
eq_(self.callback_values, [0]) #callback triggered
for i in xrange(1, 5):
self.reactor.call_asap(self._callback, i)
self.reactor.run_one_step()
eq_(self.callback_values, range(i + 1))
def test_minitwisted_crashed(self):
self.reactor.call_asap(self._crashing_callback)
assert_raises(CrashError, self.reactor.run_one_step)
def test_on_datagram_received_callback(self):
eq_(self.datagrams_received, [])
self.reactor.run_one_step()
eq_(self.datagrams_received, [])
datagram = Datagram(DATA1, tc.SERVER_ADDR)
# This is equivalent to sending a datagram to reactor
self.reactor.s.put_datagram_received(datagram)
self.reactor.run_one_step()
eq_(len(self.datagrams_received), 1)
eq_(self.datagrams_received[0], datagram)
def test_block_flood(self):
from floodbarrier import MAX_PACKETS_PER_PERIOD as FLOOD_LIMIT
for _ in xrange(FLOOD_LIMIT * 2):
self.reactor.s.put_datagram_received(Datagram(DATA1, tc.SERVER_ADDR))
for i in xrange(FLOOD_LIMIT):
eq_(len(self.datagrams_received), i)
self.reactor.run_one_step()
eq_(len(self.datagrams_received), FLOOD_LIMIT)
for i in xrange(FLOOD_LIMIT):
#.........这里部分代码省略.........
示例9: setup
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class TestMinitwisted:
def setup(self):
global time
#TODO: mock time and socket
#time = minitwisted.time = MockTime()
#minitwisted.socket = MockSocket()
self.lock = threading.Lock()
self.datagrams_received = []
self.callback_order = []
self.client_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
self.server_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
self.client_s = self.client_r.listen_udp(tc.CLIENT_ADDR[1],
self.on_datagram_received)
self.server_s = self.server_r.listen_udp(tc.SERVER_ADDR[1],
self.on_datagram_received)
self.client_r.start()
self.server_r.start()
def test_listen_upd(self):
r = ThreadedReactor()
r.start()
logger.warning(''.join(
('TESTING LOGS ** IGNORE EXPECTED WARNING ** ',
'(udp_listen has not been called)')))
self.client_r.sendto(DATA, tc.SERVER_ADDR)
while 1: #waiting for data
with self.lock:
if self.datagrams_received:
break
time.sleep(tc.TASK_INTERVAL)
with self.lock:
first_datagram = self.datagrams_received.pop(0)
logger.debug('first_datagram: %s, %s' % (
first_datagram,
(DATA, tc.CLIENT_ADDR)))
assert first_datagram, (DATA, tc.CLIENT_ADDR)
r.stop()
def test_network_callback(self):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
time.sleep(tc.TASK_INTERVAL)
with self.lock:
first_datagram = self.datagrams_received.pop(0)
logger.debug('first_datagram: %s, %s' % (
first_datagram,
(DATA, tc.CLIENT_ADDR)))
assert first_datagram, (DATA, tc.CLIENT_ADDR)
def test_block_flood(self):
from floodbarrier import MAX_PACKETS_PER_PERIOD as FLOOD_LIMIT
for _ in xrange(FLOOD_LIMIT):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
for _ in xrange(10):
self.client_r.sendto(DATA, tc.SERVER_ADDR)
logger.warning(
"TESTING LOGS ** IGNORE EXPECTED WARNING **")
time.sleep(tc.TASK_INTERVAL)
return
######################################
with self.lock:
logger.debug('datagram processed: %d/%d' % (
len(self.datagrams_received),
FLOOD_LIMIT))
print len(self.datagrams_received)
assert len(self.datagrams_received) <= FLOOD_LIMIT
def test_call_later(self):
self.client_r.call_later(.13, self.callback_f, 1)
self.client_r.call_later(.11, self.callback_f, 2)
self.client_r.call_later(.01, self.callback_f, 3)
task4 = self.client_r.call_later(.01, self.callback_f, 4)
task4.cancel()
time.sleep(.03)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [3])
self.callback_order = []
self.client_r.call_now(self.callback_f, 5)
time.sleep(.03)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [5])
self.callback_order = []
task6 = self.client_r.call_later(.03, self.callback_f, 6)
task6.cancel()
time.sleep(.1)
with self.lock:
logger.debug('callback_order: %s' % self.callback_order)
eq_(self.callback_order, [2, 1])
def test_network_and_delayed(self):
self.client_r.call_later(.2, self.callback_f, 0)
self.client_r.call_now(self.callback_f, 1)
task2 = self.client_r.call_later(.2, self.callback_f, 2)
with self.lock:
eq_(self.callback_order, [])
time.sleep(.1)
#.........这里部分代码省略.........
示例10: Controller
# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import stop [as 别名]
class Controller():
def __init__(self, dht_addr, state_path, routing_m_mod, lookup_m_mod, private_dht_name):
message.private_dht_name = private_dht_name
self.state_filename = os.path.join(state_path, STATE_FILENAME)
self.load_state()
if not self._my_id:
self._my_id = identifier.RandomId()
self._my_node = Node(dht_addr, self._my_id)
self._tracker = tracker.Tracker()
self._token_m = token_manager.TokenManager()
self._reactor = ThreadedReactor()
self._reactor.listen_udp(self._my_node.addr[1], self._on_datagram_received)
self._querier = Querier(self._my_id)
bootstrap_nodes = self.loaded_nodes or BOOTSTRAP_NODES
del self.loaded_nodes
self._routing_m = routing_m_mod.RoutingManager(self._my_node, bootstrap_nodes)
self._lookup_m = lookup_m_mod.LookupManager(self._my_id)
current_time = time.time()
self._next_maintenance_ts = current_time
self._next_save_state_ts = current_time + SAVE_STATE_DELAY
self._running = False
def start(self):
self._running = True
self._reactor.start()
self._main_loop()
def stop(self):
self._reactor.stop()
def save_state(self):
rnodes = self._routing_m.get_main_rnodes()
f = open(self.state_filename, 'w')
f.write('%r\n' % self._my_id)
for rnode in rnodes:
f.write('%d\t%r\t%s\t%d\t%f\n' % (self._my_id.log_distance(rnode.id),
rnode.id,
rnode.addr[0],
rnode.addr[1],
rnode.rtt * 1000))
f.close()
def load_state(self):
self._my_id = None
self.loaded_nodes = []
try:
f = open(self.state_filename)
except IOError:
return
try:
hex_id = f.readline().strip()
self._my_id = Id(hex_id)
for line in f:
_, hex_id, ip, port, _ = line.split()
addr = (ip, int(port))
node_ = Node(addr, Id(hex_id))
self.loaded_nodes.append(node_)
f.close()
except:
self._my_id = None
self.loaded_nodes = []
logger.error('state.dat is corrupted')
def get_peers(self, lookup_id, info_hash, callback_f, bt_port = 0):
logger.critical('get_peers %d %r' % (bt_port, info_hash))
if time.time() > self._next_maintenance_ts + 1:
logger.critical('minitwisted crashed or stopped!')
return
peers = self._tracker.get(info_hash)
if peers:
callback_f(lookup_id, peers)
log_distance = info_hash.log_distance(self._my_id)
bootstrap_rnodes = self._routing_m.get_closest_rnodes(log_distance, None, True)
lookup_obj = self._lookup_m.get_peers(lookup_id, info_hash, callback_f, bt_port)
lookup_queries_to_send = lookup_obj.start(bootstrap_rnodes)
self._send_queries(lookup_queries_to_send)
return len(lookup_queries_to_send)
def print_routing_table_stats(self):
self._routing_m.print_stats()
def _main_loop(self):
current_time = time.time()
if current_time > self._next_maintenance_ts:
maintenance_delay, queries_to_send, maintenance_lookup_target = self._routing_m.do_maintenance()
self._send_queries(queries_to_send)
if maintenance_lookup_target:
log_distance = maintenance_lookup_target.log_distance(self._my_id)
bootstrap_nodes = self._routing_m.get_closest_rnodes(log_distance, None, True)
lookup_obj = self._lookup_m.maintenance_lookup(maintenance_lookup_target)
lookup_queries_to_send = lookup_obj.start(bootstrap_nodes)
self._send_queries(lookup_queries_to_send)
self._next_maintenance_ts = current_time + maintenance_delay
if current_time > self._next_save_state_ts:
self.save_state()
self._next_save_state_ts = current_time + SAVE_STATE_DELAY
#.........这里部分代码省略.........