本文整理汇总了Python中zmq.eventloop.ioloop.PeriodicCallback.start方法的典型用法代码示例。如果您正苦于以下问题:Python PeriodicCallback.start方法的具体用法?Python PeriodicCallback.start怎么用?Python PeriodicCallback.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq.eventloop.ioloop.PeriodicCallback
的用法示例。
在下文中一共展示了PeriodicCallback.start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeviceRep
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class DeviceRep(object):
"""
Helper class to represent a device to a worker
"""
def __init__(self, device_id, state='unknown'):
self.id = device_id
self.state = state
self.curr_liveness = CLIENT_HB_LIVENESS
self.hb_timer = PeriodicCallback(self.heartbeat, CLIENT_HB_INTERVAL)
self.hb_timer.start()
return
def heartbeat(self):
if self.curr_liveness > 0:
self.curr_liveness -= 1
if self.curr_liveness == 0:
self.state = 'dead'
return
def on_message_received(self):
self.curr_liveness = CLIENT_HB_LIVENESS
return
def is_alive(self):
return self.curr_liveness > 0
def get_state(self):
return self.state
def shutdown(self):
self.hb_timer.stop()
self.hb_timer = None
示例2: WorkerRep
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class WorkerRep(object):
"""Helper class to represent a worker in the broker.
Instances of this class are used to track the state of the attached worker
and carry the timers for incomming and outgoing heartbeats.
:type wid: str
:param wid: the worker id.
:param service: service this worker serves
:type service: str
:param stream: the ZMQStream used to send messages
:type stream: ZMQStream
"""
def __init__(self, wid, service, stream):
self.id = wid
self.service = service
self.multicasts = []
self.curr_liveness = HB_LIVENESS
self.stream = stream
self.last_hb = 0
self.hb_out_timer = PeriodicCallback(self.send_hb, HB_INTERVAL)
self.hb_out_timer.start()
return
def send_hb(self):
"""Called on every HB_INTERVAL.
Decrements the current liveness by one.
Sends heartbeat to worker.
"""
self.curr_liveness -= 1
msg = [ self.id, b'', MDP_WORKER_VERSION, b'\x05' ]
self.stream.send_multipart(msg)
return
def on_heartbeat(self):
"""Called when a heartbeat message from the worker was received.
Sets current liveness to HB_LIVENESS.
"""
self.curr_liveness = HB_LIVENESS
return
def is_alive(self):
"""Returns True when the worker is considered alive.
"""
return self.curr_liveness > 0
def shutdown(self):
"""Cleanup worker.
Stops timer.
"""
self.hb_out_timer.stop()
self.hb_out_timer = None
self.stream = None
return
示例3: main
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def main(pat):
fname = find_connection_file(pat)
with open(fname) as f:
cfg = json.load(f)
url = "%s://%s:%s" % (cfg.get('transport', 'tcp'), cfg['ip'], cfg['iopub_port'])
session = Session(key=cfg['key'])
ctx = zmq.Context.instance()
sub = ctx.socket(zmq.SUB)
sub.subscribe = b''
sub.connect(url)
# import IPython
# IPython.embed()
# return
stream = ZMQStream(sub)
stream.on_recv(lambda msg_list: log_msg(session, msg_list))
pc = PeriodicCallback(print_time, 5 * 60 * 1000)
pc.start()
IOLoop.instance().start()
示例4: run
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def run(self):
_period = 1000 * self.configreader["services"][self.name]["checkmail_interval"]
beat = PeriodicCallback(self.cmd_processmails,
_period,
io_loop=self.ioloop)
beat.start()
Service.run(self)
示例5: BufferModule
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class BufferModule(base.ZmqProcess):
def __init__(self, recv_addr, send_addr, recv_title, send_title, state_handler, period=1000):
super().__init__()
self.sub_stream = None
self.timer = None
self.recv_addr = recv_addr
self.recv_title = recv_title
self.state_handler = state_handler
self.period = period
self.callback = Callback(Publisher(send_addr, send_title), self.state_handler)
def setup(self):
super().setup()
self.sub_stream, _ = self.stream(zmq.SUB, self.recv_addr, bind=False, subscribe=self.recv_title.encode('utf-8'))
self.sub_stream.on_recv(SubStreamHandler(self.sub_stream, self.stop, self.state_handler))
self.timer = PeriodicCallback(self.callback, self.period, self.loop)
def run(self):
self.setup()
print('Start loop!')
self.timer.start()
self.loop.start()
def stop(self):
self.loop.stop()
示例6: TaskState
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class TaskState (object):
""" Tracks task state (with help of watchdog) """
log = skytools.getLogger ('d:TaskState')
def __init__ (self, uid, name, info, ioloop, cc, xtx):
self.uid = uid
self.name = name
self.info = info
self.pidfile = info['config']['pidfile']
self.ioloop = ioloop
self.cc = cc
self.xtx = xtx
self.timer = None
self.timer_tick = 1
self.heartbeat = False
self.start_time = None
self.dead_since = None
def start (self):
self.start_time = time.time()
self.timer = PeriodicCallback (self.watchdog, self.timer_tick * 1000, self.ioloop)
self.timer.start()
def stop (self):
try:
self.log.info ('Signalling %s', self.name)
skytools.signal_pidfile (self.pidfile, signal.SIGINT)
except:
self.log.exception ('signal_pidfile failed: %s', self.pidfile)
def watchdog (self):
live = skytools.signal_pidfile (self.pidfile, 0)
if live:
self.log.debug ('%s is alive', self.name)
if self.heartbeat:
self.send_reply ('running')
else:
self.log.info ('%s is over', self.name)
self.dead_since = time.time()
self.timer.stop()
self.timer = None
self.send_reply ('stopped')
def ccpublish (self, msg):
assert isinstance (msg, TaskReplyMessage)
cmsg = self.xtx.create_cmsg (msg)
cmsg.send_to (self.cc)
def send_reply (self, status, feedback = {}):
msg = TaskReplyMessage(
req = 'task.reply.%s' % self.uid,
handler = self.info['task']['task_handler'],
task_id = self.info['task']['task_id'],
status = status,
feedback = feedback)
self.ccpublish (msg)
示例7: start
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def start(self):
loop = self.loop
loop.add_handler(self.udp.handle.fileno(), self.handle_beacon, loop.READ)
stream = ZMQStream(self.pipe, loop)
stream.on_recv(self.control_message)
pc = PeriodicCallback(self.send_ping, PING_INTERVAL * 1000, loop)
pc.start()
pc = PeriodicCallback(self.reap_peers, PING_INTERVAL * 1000, loop)
pc.start()
loop.start()
示例8: loop
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def loop():
self.__ioloop = IOLoop()
queue_socket = self.__context.socket(zmq.PULL)
queue_socket.connect(self.message_address)
queue_stream = ZMQStream(queue_socket, self.__ioloop)
worker_socket = self.__context.socket(zmq.DEALER)
for address in self.active_connections:
worker_socket.connect(address)
worker_stream = ZMQStream(worker_socket, self.__ioloop)
def receive_response(message, response_override=None):
self.__queued_messages.pop(message[1], None)
self.__message_timeouts.pop(message[1], None)
callback = self.__callbacks.pop(message[1], None)
if callback:
try:
callback(response_override or self.loads(self.decompress(message[2])))
except Exception as e:
self.log_error(e)
callback({'error': e})
worker_stream.on_recv(receive_response)
def queue_message(message):
if message[0]:
if message[0] == WORKER_SOCKET_CONNECT and message[2] not in self.active_connections:
self.active_connections.add(message[2])
worker_stream.socket.connect(message[2])
elif message[0] == WORKER_SOCKET_DISCONNECT and message[2] in self.active_connections:
self.active_connections.remove(message[2])
worker_stream.socket.disconnect(message[2])
return
self.__queued_messages[message[1]] = (time(), message)
try:
worker_stream.send_multipart(message)
except Exception as e:
self.log_error(e)
queue_stream.on_recv(queue_message)
def timeout_message():
now = time()
for message, retry in [(item[1], self.__message_auto_retry.get(item[1][1], self.__auto_retry)) for item, t in ((i, self.__message_timeouts.get(i[1][1], self.__timeout)) for i in self.__queued_messages.itervalues()) if t >= 0 and (item[0] + t < now)]:
if retry:
logging.info('Worker timeout, requeuing ' + message[1])
queue_message(message)
else:
receive_response(('', message[1]), {'error': 'timeout'})
timeout_callback = PeriodicCallback(timeout_message, int(abs(self.__timeout * 1000.0)), io_loop = self.__ioloop)
timeout_callback.start()
self.__ioloop.start()
self.__thread = None
示例9: on_run
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def on_run(self, ctx, io_loop, socks, streams):
z = ZmqRpcProxy(self._uris['manager_rpc'])
self._data = self._initial_data()
self._remote_handlers = set(z.available_handlers())
env = OrderedDict([
('ctx', ctx),
('io_loop', io_loop),
('socks', socks),
('streams', streams),
])
f = functools.partial(self.timer__monitor_heartbeats, env)
callback = PeriodicCallback(f, 1000, io_loop=io_loop)
callback.start()
f = functools.partial(self.timer__grim_reaper, env)
callback = PeriodicCallback(f, 5000, io_loop=io_loop)
callback.start()
示例10: Delay
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class Delay (CCHandler):
""" Delays all received messages, then dispatches them to another handler. """
CC_ROLES = ['local', 'remote']
log = skytools.getLogger ('h:Delay')
tick = 250 # ms
def __init__ (self, hname, hcf, ccscript):
super(Delay, self).__init__(hname, hcf, ccscript)
self.fwd_hname = self.cf.get ('forward-to')
self.delay = self.cf.getint ('delay', 0)
self.fwd_handler = ccscript.get_handler (self.fwd_hname)
self.queue = collections.deque()
self.timer = PeriodicCallback (self.process_queue, self.tick, self.ioloop)
self.timer.start()
def handle_msg (self, cmsg):
""" Got message from client -- queue it """
self.queue.append ((time.time() + self.delay, cmsg))
def process_queue (self):
now = time.time()
try:
while (self.queue[0][0] <= now):
at, cmsg = self.queue.popleft()
size = cmsg.get_size()
try:
self.fwd_handler.handle_msg (cmsg)
stat = 'ok'
except Exception:
self.log.exception ('crashed, dropping msg: %s', cmsg.get_dest())
stat = 'crashed'
self.stat_inc ('delay.count')
self.stat_inc ('delay.bytes', size)
self.stat_inc ('delay.count.%s' % stat)
self.stat_inc ('delay.bytes.%s' % stat, size)
except IndexError:
pass
示例11: start
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def start(self):
"""Initialize and start the event loop. Listen for ZMQ control
messages."""
ctx = zmq.Context()
socket = ctx.socket(zmq.PAIR)
socket.bind("tcp://*:{}".format(settings.ZMQ_CONTROL_PORT))
logserver = LogServer()
logserver.listen(settings.TCP_LOGGING_PORT)
loop = self.loop
stream = ZMQStream(socket, loop)
stream.on_recv(self.handle_ctrl_msg)
# TODO: Stop loop and restart on CONFIG reread just to get possible new
# EVENT_POLL_INTERVAL setting?
pc = PeriodicCallback(self.chime, self.EVENT_POLL_INTERVAL * 1E3, loop)
pc.start()
loop.start()
示例12: run_sock_configs
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def run_sock_configs(sock_configs, on_run=None, control_pipe=None):
ctx, io_loop, socks, streams = get_run_context(sock_configs)
def _on_run():
on_run(ctx, io_loop, socks, streams)
if on_run:
io_loop.add_callback(_on_run)
def watchdog():
if control_pipe.poll():
io_loop.stop()
if control_pipe:
callback = PeriodicCallback(watchdog, 500, io_loop=io_loop)
callback.start()
try:
io_loop.start()
except KeyboardInterrupt:
pass
示例13: start
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def start(root):
global loop, path, checkup_periodic, control_port
path = root
print('Starting.')
# add HOSTS to /etc/hosts
add_hosts(HOSTS)
# define context
ctx = zmq.Context()
# create ioloop
loop = zmq.eventloop.ioloop.IOLoop()
# connect req to mongrel2 control port
c = ctx.socket(zmq.REQ)
c.connect(M2_CONTROL_PORT)
control_port = ZMQStream(c, io_loop=loop)
# define 'checkup' interval
checkup_periodic = PeriodicCallback(send_checkup,
CHECKUP_INTERVAL,
io_loop=loop)
# load mongrel2 config
load_mongrel()
# kill PID if server didn't get shut down at close of last run
kill_mongrel_with_pid(M2_PID_PATH)
# start mongrel2 with m2sh
start_mongrel()
# start the loop
checkup_periodic.start()
loop.start()
示例14: loop
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
def loop():
self.__ioloop = IOLoop()
queue_socket = self.__context.socket(zmq.PULL)
queue_socket.connect(self.message_address)
queue_stream = ZMQStream(queue_socket, self.__ioloop)
worker_socket = self.__context.socket(zmq.DEALER)
worker_socket.connect(self.address)
worker_stream = ZMQStream(worker_socket, self.__ioloop)
def receive_response(message):
self.__queued_messages.pop(message[1], None)
self.__message_timeouts.pop(message[1], None)
callback = self.__callbacks.pop(message[1], None)
if callback:
try:
callback(self.loads(self.decompress(message[2])))
except Exception as e:
self.log_error(e)
worker_stream.on_recv(receive_response)
def queue_message(message):
self.__queued_messages[message[1]] = (time() * 1000, message)
try:
worker_stream.send_multipart(message)
except Exception as e:
self.log_error(e)
queue_stream.on_recv(queue_message)
def requeue_message():
now = time() * 1000
for message in (item[1] for item in self.__queued_messages.itervalues() if item[0] + self.__message_timeouts.get(item[1][1], self.__retry_ms) < now):
queue_message(message)
requeue_callback = PeriodicCallback(requeue_message, self.__retry_ms, io_loop = self.__ioloop)
requeue_callback.start()
self.__ioloop.start()
self.__thread = None
示例15: DemoApp
# 需要导入模块: from zmq.eventloop.ioloop import PeriodicCallback [as 别名]
# 或者: from zmq.eventloop.ioloop.PeriodicCallback import start [as 别名]
class DemoApp(ZMQProcess):
def __init__ (self):
super(DemoApp, self).__init__()
def setup(self):
super(DemoApp, self).setup()
self.pub, self.pub_addr = self.stream(zmq.PUB, 'tcp://127.0.0.1:%(port)s', True)
self.sub, sub_addr = self.stream(zmq.SUB, self.pub_addr, False,
callback=DemoHandler())
self.heartbeat = PeriodicCallback(self.ping, 1000, self.loop)
def ping(self):
print 'SEND PING'
self.pub.send_multipart(['ping', json.dumps(['ping', time.time()])])
def local_run(self):
print 'START HEARTBEAT'
self.heartbeat.start()
def stop(self):
self.heartbeat.stop()
self.loop.stop()