本文整理汇总了Python中sender.Sender.start方法的典型用法代码示例。如果您正苦于以下问题:Python Sender.start方法的具体用法?Python Sender.start怎么用?Python Sender.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sender.Sender
的用法示例。
在下文中一共展示了Sender.start方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: emulate
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def emulate(args):
'''
Starts the Sender/Receiver process threads, sleeps for
args.RUNTIME, and terminates both threads. Returns a tuple of
lists: (s_log, r_log), where s_log is sender's log and r_log is
receiver's log.
'''
args.SENDER_TIMEOUT = float(args.SENDER_TIMEOUT)
args.RUNTIME = float(args.RUNTIME)
assert args.SENDER_TIMEOUT > 0
assert args.RUNTIME > 0
s = Sender(args.SENDER_TIMEOUT)
r = Receiver()
s.set_remote_endpoint(r)
r.set_remote_endpoint(s)
r.daemon = True
s.daemon = True
# Start the sender process.
s.start()
r.start()
try:
time.sleep(args.RUNTIME)
except KeyboardInterrupt:
print "Interrupted, terminating."
# We have to be careful with terminating the two threads, as they
# can only exit in specific states, and we can cause a deadlock.
# First, we terminate the sender, and wait for it to finish. Once
# this happens, we know that the receiver is in an ok terminal
# state, so we terminate it right after.
s.terminate()
s.join()
r.terminate()
r.join()
#s.log.append((([0,0], "S-TERM")))
#r.log.append((([0,0], "R-TERM")))
# At this point, the sender is not generating any more
# messages. But, we might have some oustanding messages in
# receiver's queue. So, process these, if any:
while not r.rx_queue.empty():
# Receive msg and generate any outstanding acks.
r.transition()
r.transition()
r.transition()
# Consume any outstanding acks on the sender's side.
s.consume_acks()
return (s.log, r.log)
示例2: main
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def main():
global indexer, uploader, sender, receiver, downloader
setup_signals()
logging.info("Asink client started at %s" %
(time.strftime("%a, %d %b %Y %X GMT", time.gmtime())))
#create all threads which will be used to process events
indexer = Indexer()
uploader = Uploader()
sender = Sender()
receiver = Receiver()
downloader = Downloader()
#create and set up queues which are used to pass events between threads
uploader_queue = Queue()
indexer.uploader_queue = uploader_queue
uploader.queue = uploader_queue
#set on watcher when initialized
sender_queue = Queue()
uploader.sender_queue = sender_queue
sender.queue = sender_queue
downloader_queue = Queue()
receiver.downloader_queue = downloader_queue
downloader.queue = downloader_queue
#setup storage provider
storage = setup_storage()
uploader.storage = storage.clone()
downloader.storage = storage
#start all threads
watcher.start_watching(uploader_queue)
indexer.start()
uploader.start()
sender.start()
receiver.start()
downloader.start()
#sleep until signaled, which will call sig_handler
while True:
time.sleep(86400) #= 24 hours just for fun
示例3: main
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def main():
create_queue()
sender = Sender()
receiver = Receiver()
sender.start()
receiver.start()
示例4: run
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def run(self):
while True:
try:
self.rep_ip = self._replication_interface()
self.uuid = self._my_uuid()
break
except:
msg = ('Failed to get replication interface or uuid. '
'Aborting.')
return logger.error(msg)
ctx = zmq.Context()
# fs diffs are sent via this publisher.
rep_pub = ctx.socket(zmq.PUB)
rep_pub.bind('tcp://%s:%d' % (self.rep_ip, self.data_port))
# synchronization messages are received in this pull socket
meta_pull = ctx.socket(zmq.PULL)
meta_pull.RCVTIMEO = 100
meta_pull.bind('tcp://%s:%d' % (self.rep_ip, self.meta_port))
total_sleep = 0
while True:
if (os.getppid() != self.ppid):
logger.error('Parent exited. Aborting.')
break
while(not self.pubq.empty()):
msg = self.pubq.get()
rep_pub.send(msg)
# check for any recv's coming
num_msgs = 0
while (num_msgs < 1000):
try:
self.recv_meta = meta_pull.recv_json()
num_msgs = num_msgs + 1
snap_id = self.recv_meta['id']
if (self.recv_meta['msg'] == 'begin'):
rw = Receiver(self.recv_meta)
self.receivers[snap_id] = rw
rw.start()
elif (snap_id not in self.senders):
logger.error('Unknown snap_id(%s) received. Ignoring'
% snap_id)
else:
self.senders[snap_id].q.put(self.recv_meta)
except zmq.error.Again:
break
self._prune_workers((self.receivers, self.senders))
if (int(time.time()) - self.prune_time > 3600):
self.prune_time = int(time.time())
for rs in ReplicaShare.objects.all():
prune_receive_trail(rs.id, logger)
for r in Replica.objects.all():
prune_replica_trail(r.id, logger)
if (total_sleep >= 60 and len(self.senders) < 50):
try:
for r in get_replicas(logger):
rt = get_replica_trail(r.id, logger)
now = datetime.utcnow().replace(second=0,
microsecond=0,
tzinfo=utc)
sw = None
snap_name = 'replication'
rt2 = ReplicaTrail.objects.filter().order_by('-id')
if (len(rt2) != 0):
snap_name = ('%s_%d' % (snap_name, rt2[0].id + 1))
else:
snap_name = ('%s_1' % snap_name)
snap_id = ('%s_%s_%s_%s' %
(self.uuid, r.pool, r.share, snap_name))
if (len(rt) == 0):
logger.debug('new sender for snap: %s' % snap_id)
sw = Sender(r, self.rep_ip, self.pubq, Queue(),
snap_name, self.meta_port,
self.data_port, r.meta_port, self.uuid,
snap_id)
elif (rt[0].status == 'succeeded'):
if (((now - rt[0].end_ts).total_seconds() >
(r.frequency * 60))):
logger.debug('incremental sender for snap: %s'
% snap_id)
sw = Sender(r, self.rep_ip, self.pubq, Queue(),
snap_name, self.meta_port,
self.data_port, r.meta_port,
self.uuid, snap_id, rt[0])
else:
continue
elif (rt[0].status == 'pending'):
prev_snap_id = ('%s_%s_%s_%s' % (self.uuid,
r.pool, r.share, rt[0].snap_name))
if (prev_snap_id in self.senders):
logger.debug('send process ongoing for snap: '
'%s' % snap_id)
continue
#.........这里部分代码省略.........
示例5: run_sender
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def run_sender(self, queue):
sender_config = self.config.get("sender")
sender_config["log_format"] = self.log_format
sender = Sender(queue=queue, shutdown=self.shutdown_event, config=sender_config)
self.processes.append(sender)
sender.start()
示例6: run
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def run(self):
sleep_time = 0
while True:
if (os.getppid() != self.ppid):
logger.info('parent exited. aborting.')
break
try:
self.rep_ip = self._replication_interface()
break
except:
time.sleep(1)
sleep_time = sleep_time + 1
if (sleep_time % 30 == 0):
msg = ('Failed to get replication interface for last %d'
' seconds' % sleep_time)
logger.info('failed to get replication interface')
ctx = zmq.Context()
#fs diffs are sent via this publisher.
rep_pub = ctx.socket(zmq.PUB)
rep_pub.bind('tcp://%s:%d' % (self.rep_ip, self.data_port))
#synchronization messages are received in this pull socket
meta_pull = ctx.socket(zmq.PULL)
meta_pull.RCVTIMEO = 100
meta_pull.bind('tcp://%s:%d' % (self.rep_ip, self.meta_port))
total_sleep = 0
while True:
if (os.getppid() != self.ppid):
logger.info('parent exited. aborting.')
break
while(not self.pubq.empty()):
msg = self.pubq.get()
rep_pub.send(msg)
#check for any recv's coming
try:
self.recv_meta = meta_pull.recv_json()
snap_id = self.recv_meta['id']
if (self.recv_meta['msg'] == 'begin'):
logger.info('begin received. meta: %s' % self.recv_meta)
rw = Receiver(self.recv_meta, Queue())
self.receivers[snap_id] = rw
rw.start()
elif (self.recv_meta['msg'] == 'begin_ok'):
self.senders[snap_id].q.put('send')
elif (self.recv_meta['msg'] == 'receive_ok' or
self.recv_meta['msg'] == 'receive_error'):
self.senders[snap_id].q.put(self.recv_meta['msg'])
except zmq.error.Again:
pass
self._prune_workers((self.receivers, self.senders))
if (total_sleep >= 60 and len(self.senders) < 50):
logger.info('scanning for replicas')
for r in Replica.objects.filter(enabled=True):
rt = ReplicaTrail.objects.filter(replica=r).order_by('-snapshot_created')
now = datetime.utcnow().replace(second=0,
microsecond=0,
tzinfo=utc)
sw = None
snap_name = ('%s_replica_snap' % r.share)
if (len(rt) == 0):
snap_name = ('%s_1' % snap_name)
sw = Sender(r, self.rep_ip, self.pubq, Queue(),
snap_name)
elif (rt[0].status == 'succeeded' and
(now - rt[0].end_ts).total_seconds() >
r.frequency):
snap_name = ('%s_%d' % (snap_name, rt[0].id + 1))
sw = Sender(r, self.rep_ip, self.pubq, Queue(),
snap_name, rt[0])
else:
continue
snap_id = ('%s_%s_%s_%s' %
(self.rep_ip, r.pool, r.share, snap_name))
self.senders[snap_id] = sw
sw.daemon = True
sw.start()
total_sleep = 0
time.sleep(1)
total_sleep = total_sleep + 1
示例7: VpnServer
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
class VpnServer(object):
def __init__(self, port, shared_key, connected_callback, broken_conn_callback,
debug_continue, debug, app):
self.port = port
self.shared_key = shared_key
self.connected_callback = connected_callback
self.broken_conn_callback = broken_conn_callback
self.send_queue = Queue()
self.receive_queue = Queue()
self.authenticated = False
self.debug_continue = debug_continue
self.debug = debug
self.waiting = True
self.sender = None
self.receiver = None
self.is_server = True
self.sessionkey=''
self.app = app
def setup(self):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
return (-1, "Could not create socket")
try:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('', self.port))
Logger.log("Listening for connections...", self.is_server)
self.socket.listen(1)
except socket.error:
return (-1, "Could not bind socket to port " + str(self.port))
return (0, "VPN server set to listen on port " + str(self.port))
def send(self, msg):
if (self.authenticated):
Logger.log("sessionkey: " +self.sessionkey, self.is_server)
emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey())
self.send_queue.put(emsg)
Logger.log("Put message on send queue: "+ msg, self.is_server)
else:
self.send_queue.put(msg)
Logger.log("Put message on send queue: "+ msg, self.is_server)
def receive(self):
if not self.receive_queue.empty():
msg = self.receive_queue.get()
if (self.authenticated):
msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey())
if valid is False:
return None
Logger.log("Decrypted msg: "+ msg, self.is_server)
return msg
else:
return None
def start(self, callback=None):
self.listener = Listener(self.socket, self.shared_key, self, self.connected_callback, self.app)
self.listener.start()
def bind(self, client_socket):
self.debug_continue.disabled = self.debug
self.sender = Sender(client_socket, self.send_queue, self)
self.receiver = Receiver(client_socket, self.receive_queue, self)
self.sender.start()
self.receiver.start()
def clear_queues(self):
self.receive_queue.queue.clear()
self.send_queue.queue.clear()
def broken_conn(self):
Logger.log("Broken connection", self.is_server)
self.send_queue.queue.clear()
self.receive_queue.queue.clear()
self.sender.close()
self.receiver.close()
self.waiting = True
self.authenticated = False
if (self.listener):
self.listener.broken_conn()
def close(self):
Logger.log("Connection closing", self.is_server)
self.send_queue.queue.clear()
self.receive_queue.queue.clear()
self.listener.close()
self.socket.close()
self.authenticated = False
if self.sender:
self.sender.close()
if self.receiver:
self.receiver.close()
示例8: Sender
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
for rto in rt:
if (rto.status == 'succeeded'):
prev_rt = rto
break
sw = Sender(r, self.rep_ip, self.pubq, Queue(),
snap_name, self.meta_port,
self.data_port, r.meta_port,
self.uuid, snap_id, prev_rt)
else:
logger.error('unknown replica trail status: %s. '
'ignoring snap: %s' %
(rt[0].status, snap_id))
continue
self.senders[snap_id] = sw
sw.daemon = True
sw.start()
except DatabaseError, e:
e_msg = ('Error getting the list of enabled replica '
'tasks. Moving on. Exception: %s' % e.__str__())
logger.error(e_msg)
time.sleep(1)
total_sleep = total_sleep + 1
def main():
rs = ReplicaScheduler()
rs.start()
logger.debug('Started Replica Scheduler')
rs.join()
示例9: Sender
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
#!/usr/bin/python
# -*- coding: utf-8 -*-
from sender import Sender
from moderator import Moderator
from receiver import receive
s = Sender()
m = Moderator(s.send)
m.start()
s.start()
receive(m.moderate, s.send)
示例10: run
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
def run(self):
while True:
try:
self.rep_ip = self._replication_interface()
self.uuid = self._my_uuid()
break
except:
msg = "Failed to get replication interface or uuid. " "Aborting."
return logger.error(msg)
ctx = zmq.Context()
# fs diffs are sent via this publisher.
rep_pub = ctx.socket(zmq.PUB)
rep_pub.bind("tcp://%s:%d" % (self.rep_ip, self.data_port))
# synchronization messages are received in this pull socket
meta_pull = ctx.socket(zmq.PULL)
meta_pull.RCVTIMEO = 100
meta_pull.bind("tcp://%s:%d" % (self.rep_ip, self.meta_port))
total_sleep = 0
while True:
if os.getppid() != self.ppid:
logger.error("Parent exited. Aborting.")
break
while not self.pubq.empty():
msg = self.pubq.get()
rep_pub.send(msg)
# check for any recv's coming
num_msgs = 0
while num_msgs < 1000:
try:
self.recv_meta = meta_pull.recv_json()
num_msgs = num_msgs + 1
snap_id = self.recv_meta["id"]
if self.recv_meta["msg"] == "begin":
rw = Receiver(self.recv_meta)
self.receivers[snap_id] = rw
rw.start()
elif snap_id not in self.senders:
logger.error("Unknown snap_id(%s) received. Ignoring" % snap_id)
else:
self.senders[snap_id].q.put(self.recv_meta)
except zmq.error.Again:
break
self._prune_workers((self.receivers, self.senders))
if int(time.time()) - self.prune_time > 3600:
self.prune_time = int(time.time())
for rs in ReplicaShare.objects.all():
prune_receive_trail(rs.id, logger)
for r in Replica.objects.all():
prune_replica_trail(r.id, logger)
if total_sleep >= 60 and len(self.senders) < 50:
try:
for r in get_replicas(logger):
rt = get_replica_trail(r.id, logger)
now = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=utc)
sw = None
snap_name = "replication"
rt2 = ReplicaTrail.objects.filter().order_by("-id")
if len(rt2) != 0:
snap_name = "%s_%d" % (snap_name, rt2[0].id + 1)
else:
snap_name = "%s_1" % snap_name
snap_id = "%s_%s_%s_%s" % (self.uuid, r.pool, r.share, snap_name)
if len(rt) == 0:
logger.debug("new sender for snap: %s" % snap_id)
sw = Sender(
r,
self.rep_ip,
self.pubq,
Queue(),
snap_name,
self.meta_port,
self.data_port,
r.meta_port,
self.uuid,
snap_id,
)
elif rt[0].status == "succeeded":
if (now - rt[0].end_ts).total_seconds() > (r.frequency * 60):
logger.debug("incremental sender for snap: %s" % snap_id)
sw = Sender(
r,
self.rep_ip,
self.pubq,
Queue(),
snap_name,
self.meta_port,
self.data_port,
r.meta_port,
self.uuid,
snap_id,
rt[0],
#.........这里部分代码省略.........
示例11: VpnClient
# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import start [as 别名]
class VpnClient(object):
def __init__(self, ip_addr, port, shared_key, broken_conn_callback, app):
self.ip_addr = ip_addr
self.port = port
self.shared_key = shared_key
self.broken_conn_callback = broken_conn_callback
self.send_queue = Queue()
self.receive_queue = Queue()
self.waiting = True
self.is_server=False
self.authenticated=False
self.sender = None
self.receiver = None
self.app = app
def connect(self):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error:
return (-1, "Could not create socket")
try:
self.socket.settimeout(10)
self.socket.connect((self.ip_addr, self.port))
self.waiting = False
self.auth = Authentication(self.shared_key, self, self.app, debug=True, is_server=False)
self.bind() # Added because we need the send/recv threads running for authentication
if (self.auth.mutualauth()):
print "Server Authenticated!"
Logger.log("Connected to Server", self.is_server)
self.authenticated = True
self.sessionkey = self.auth.get_sessionkey()
self.clear_queues()
return (0, "Connected to (%s, %i)" % (self.ip_addr, self.port))
else:
print "Could not authenticate"
self.authenticated = False
self.broken_conn_callback()
return (-1, "Authentication failed")
except socket.error:
self.authenticated = False
self.broken_conn_callback()
return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port))
return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port))
def clear_queues(self):
self.receive_queue.queue.clear()
self.send_queue.queue.clear()
def send(self, msg):
if (self.authenticated):
emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey())
self.send_queue.put(emsg)
Logger.log("Put message on send queue: " + msg, self.is_server)
else:
self.send_queue.put(msg)
Logger.log("Put message on send queue: " + msg, self.is_server)
def bind(self):
self.sender = Sender(self.socket, self.send_queue, self)
self.receiver = Receiver(self.socket, self.receive_queue, self)
self.sender.start()
self.receiver.start()
def close(self):
Logger.log("Connection closing", self.is_server)
self.send_queue.queue.clear()
self.receive_queue.queue.clear()
if self.sender:
self.sender.close()
if self.receiver:
self.receiver.close()
self.waiting = True
self.authenticated = False
self.auth = None
def receive(self):
if (not self.receive_queue.empty()):
msg = self.receive_queue.get()
Logger.log("Received decrypted msg: "+ msg, self.is_server)
if (self.authenticated):
msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey())
if valid is False:
return None # ignore failed CBC authentication message
Logger.log("Decrypted msg: "+ msg, self.is_server)
return msg
else:
return None