本文整理汇总了Python中zmq.Context.socket方法的典型用法代码示例。如果您正苦于以下问题:Python Context.socket方法的具体用法?Python Context.socket怎么用?Python Context.socket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq.Context
的用法示例。
在下文中一共展示了Context.socket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Leatherneck
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class Leatherneck(Thread):
def __init__(self):
super(Leatherneck, self).__init__(name="Leatherneck")
self.context = Context()
self.pull = self.context.socket(PULL)
self.pull.connect("tcp://localhost:7000")
self.push = self.context.socket(PUSH)
self.push.connect("tcp://localhost:7001")
self.poller = Poller()
self.poller.register(self.pull, POLLIN)
self._shutdown = False
for th in t_enum():
if th.name == "MainThread":
self.mainthread = th
def cleanup(self):
print "Workers exiting..."
self.push.close()
self.pull.close()
self.context.term()
def run(self):
while True:
if not self.mainthread.is_alive():
self._shutdown = True
break
socks = dict(self.poller.poll(timeout=1))
if socks.get(self.pull) == POLLIN:
msg = self.pull.recv(flags=NOBLOCK)
msg += " WORK COMPLETE, " + str(time())
self.push.send(msg, flags=NOBLOCK)
if self._shutdown:
break
self.cleanup()
示例2: run
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
def run(port):
""" Run a translations server at a specific port.
It always listens on all available network devices!
"""
context = Context(1)
sync_socket = context.socket(ROUTER)
sync_socket.bind(_SYNC_ENDPOINT)
frontend = context.socket(ROUTER)
frontend.bind("tcp://*:{}".format(port))
# Socket facing services
backend = context.socket(DEALER)
backend.bind(_REQUEST_ENDPOINT)
try:
worker_threads, worker_identities = _start_workers(
context, sync_socket, config.WORKERS, 1000)
_LOG.debug("Running device...")
try:
proxy(frontend, backend)
except KeyboardInterrupt:
print("\rShutting down...")
frontend.close()
frontend = None
_shut_down_workers(sync_socket, worker_threads, worker_identities, 5)
finally:
if frontend is not None:
frontend.close()
backend.close()
sync_socket.close()
_LOG.debug("Done")
示例3: wrapped_dispatcher
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class wrapped_dispatcher(object):
def __init__(self, enqueued=None, on_load=None):
self.queue = Queue()
kwargs = {
'queue': self.queue
}
if enqueued:
kwargs['enqueued_tasks'] = enqueued
if on_load:
kwargs['on_daemon_load'] = on_load
self.dispatcher = WrappedDispatcher(**kwargs)
self.context = None
self.sockets = {}
def __enter__(self):
self.dispatcher.start()
self.context = Context()
self.sockets['in'] = self.context.socket(PUSH)
self.sockets['out'] = self.context.socket(PULL)
self.sockets['in'].connect(settings.ZTASKD_URL)
self.sockets['out'].connect(settings.ZTASK_WORKER_URL)
return (self.queue, self.sockets['in'], self.sockets['out'])
def __exit__(self, exc_type, exc_value, traceback):
self.dispatcher.terminate()
self.context.destroy()
self.queue.close()
示例4: main
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
def main(name):
map = generate_map(name)
context = Context()
pub_socket = context.socket(PUB)
pub_socket.bind('tcp://0.0.0.0:20000')
map['pub_server'] = pub_socket
rep_socket = context.socket(REP)
rep_socket.bind('tcp://0.0.0.0:20001')
map['rep_server'] = rep_socket
manager = MissionManager()
rep_socket = map['rep_server']
manager.start_mission(LocMission(map, None))
return
while True:
req = MissionMessage()
req.ParseFromString(rep_socket.recv())
if req.type == 6:
req.type = 7
try:
if req.missionType == MissionMessage.CONNECTION:
manager.start_mission(PlaceMission(map, (0.0, 0.0), req))
elif req.missionType == MissionMessage.LOCALIZATION:
manager.start_mission(LocMission(map, req))
else:
raise ValueError('unknown mission type')
req.status = MissionMessage.ACTIVE
except RuntimeError:
req.status = MissionMessage.REJECTED
rep_socket.send(req.SerializeToString())
示例5: Leatherneck
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class Leatherneck(Thread):
def __init__(self):
super(Leatherneck, self).__init__(name="Leatherneck")
self.context = Context()
self.pull = self.context.socket(PULL)
self.pull.connect("tcp://localhost:7000")
self.push = self.context.socket(PUSH)
self.push.connect("tcp://localhost:7001")
self.poller = Poller()
self.poller.register(self.pull, POLLIN)
self._shutdown = False
def cleanup(self):
self.push.close()
self.pull.close()
self.context.term()
def run(self):
while True:
socks = dict(self.poller.poll(timeout=1))
if socks.get(self.pull) == POLLIN:
msg = self.pull.recv()
msg += " WORK COMPLETE, " + str(time())
self.push.send(msg)
if self._shutdown:
break
self.cleanup()
示例6: TestEchoService
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class TestEchoService(WindmillTestCase):
def setUp(self):
self.zmq_ctx = Context()
d = 'test_out'
if not os.path.exists(d):
os.makedirs(d)
def tearDown(self):
pass
def test_echo_service_default_behavior(self):
req_out_sock = self.zmq_ctx.socket(REQ)
req_out_sock.bind('tcp://*:8889')
t = thread_wrap_windmill('EchoService')
try:
t.start()
self.assertTrue(t.is_alive,
'The EchoService instance should have started.')
req_out_sock.send("echo, echo, echo")
msg = req_out_sock.recv()
self.assertEqual("echo, echo, echo", msg)
finally:
t.windmill.kill()
t.join(3)
self.assertFalse(t.is_alive(),
'The EchoService instance should have shutdown.')
req_out_sock.close()
def test_echo_service_options(self):
req_out_sock = self.zmq_ctx.socket(REQ)
req_out_sock.bind('tcp://*:8899')
argv = ['-m', 'pong', '--reply_sock_url', 'tcp://localhost:8899']
t = thread_wrap_windmill('EchoService', argv=argv)
try:
t.start()
self.assertTrue(t.is_alive(),
'The EchoService instance should have started.')
req_out_sock.send('ping')
msg = req_out_sock.recv()
self.assertEqual('pong', msg)
finally:
t.windmill.kill()
t.join(3)
self.assertFalse(t.is_alive(),
'The EchoService instance shold have shutdown.')
req_out_sock.close()
示例7: Master
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class Master(object):
def __init__(self, full_socket_address):
self.context = Context()
self.workers = OrderedDict()
self.overflow_launch = False
self.stats = False
self.full_socket_address = full_socket_address
self.socket_address, self.socket_port = full_socket_address.split(':')
@property
@zeroMQError
def init_pubsocket(self):
''' initialise la socket pour permettre de lancer
le benchmark via un publish
'''
self.pubsocket = self.context.socket(PUB)
self.pubsocket.bind('tcp://{}'.format(self.full_socket_address))
@property
@zeroMQError
def init_repsocket(self):
''' init la socket pour permettre de repondre à un
nouveau worker qui vient s'ajouter dynamiquement
par default ce port est fixe (55555)
'''
self.repsocket = self.context.socket(REP)
self.repsocket.bind('tcp://{}:55555'.format(self.socket_address))
@property
def wait_workers(self):
''' permet l'ajout de worker en attendand le
message pour lancer le benchmark
'''
while not self.overflow_launch:
message = loads(self.repsocket.recv_json())
# workers
if '_id' in message:
self.workers[message['_id']] = 'ready'
self.repsocket.send('ok')
sys.stdout.write('worker {} is ready\n'.format(message['_id']))
# overflow signals
elif 'overflow' in message:
self.repsocket.send('ok')
sys.stdout.write('master: launch overflow for {}\n'.format(self.workers.keys()))
self.launch_benchmark
@property
def launch_benchmark(self):
''' declenche le benchmark
'''
self.pubsocket.send('OVERFLOW')
self.workers = OrderedDict()
示例8: HomeBase
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class HomeBase(Thread):
def __init__(self):
super(HomeBase, self).__init__(name="HomeBase")
self.context = Context()
self.pull = self.context.socket(PULL)
self.pull.bind("tcp://*:7001")
self._shutdown = False
self.poller = Poller()
self.poller.register(self.pull, POLLIN)
for th in t_enum():
if th.name == "MainThread":
self.mainthread = th
def cleanup(self):
print "Home exiting..."
self.pull.close()
self.context.term()
def run(self):
while True:
if not self.mainthread.is_alive():
self._shutdown = True
break
socks = dict(self.poller.poll(timeout=1))
if socks.get(self.pull) == POLLIN:
msg = self.pull.recv(flags=NOBLOCK)
msg += ", WORK RECEIVED "
print msg
if self._shutdown:
break
self.cleanup()
示例9: DrillingWell
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class DrillingWell(Thread):
def __init__(self):
super(DrillingWell, self).__init__(name="DrillingWell")
self.context = Context()
self.push = self.context.socket(PUSH)
self.push.bind("tcp://*:7000")
self._shutdown = False
for th in t_enum():
if th.name == "MainThread":
self.mainthread = th
def cleanup(self):
print "Producer exiting..."
self.push.close()
self.context.term()
def run(self):
count = 0
while True:
if not self.mainthread.is_alive():
self._shutdown = True
break
sleep(0.01)
count += 1
self.push.send("SOMETHING " + str(count))
if self._shutdown:
break
self.cleanup()
示例10: WorkerTest
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class WorkerTest(TestCase):
"""Ensures the worker correctly handles messages
"""
def setUp(self):
self.queue = Queue()
self.context = Context()
self.socket = self.context.socket(PUSH)
self.socket.bind(settings.ZTASK_WORKER_URL)
self.worker = WrappedWorker(queue=self.queue)
self.worker.start()
def tearDown(self):
self.worker.terminate()
self.context.destroy()
def test_exec(self):
"""Tests executing a task
"""
uuid = str(uuid4())
self.socket.send_pyobj((uuid,))
self.assertEqual(
self.queue.get(),
uuid
)
self.assertTrue(self.queue.get())
self.queue.close()
示例11: Zmq_broker
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class Zmq_broker(BaseModule):
context = None
s_pub = None
pub_endpoint = None
serialize_to = None
serialize = None
def __init__(self, mod_conf, pub_endpoint, serialize_to):
from zmq import Context, PUB
BaseModule.__init__(self, mod_conf)
self.pub_endpoint = pub_endpoint
self.serialize_to = serialize_to
logger.info("[Zmq Broker] Binding to endpoint " + self.pub_endpoint)
# This doesn't work properly in init()
# sometimes it ends up beings called several
# times and the address becomes already in use.
self.context = Context()
self.s_pub = self.context.socket(PUB)
self.s_pub.bind(self.pub_endpoint)
# Load the correct serialization function
# depending on the serialization method
# chosen in the configuration.
if self.serialize_to == "msgpack":
from msgpack import Packer
packer = Packer(default=encode_monitoring_data)
self.serialize = lambda msg: packer.pack(msg)
elif self.serialize_to == "json":
self.serialize = lambda msg: json.dumps(msg, cls=SetEncoder)
else:
raise Exception("[Zmq Broker] No valid serialization method defined (Got " + str(self.serialize_to) + ")!")
# Called by Broker to say 'let's prepare yourself guy'
def init(self):
logger.info("[Zmq Broker] Initialization of the Zmq broker module")
# Publish to the ZeroMQ socket
# using the chosen serialization method
def publish(self, msg, topic=""):
from zmq import SNDMORE
data = self.serialize(msg)
self.s_pub.send(topic, SNDMORE)
self.s_pub.send(data)
# An host check have just arrived, we UPDATE data info with this
def manage_brok(self, b):
logger.debug("[Zmq Broker] Got broker update: " + str(b.data))
# Publish update data to the ZeroMQ endpoint.
msg = b.data
self.publish(msg, b.type)
# Properly close down this thing.
def do_stop(self):
self.s_pub.close()
self.context.term()
示例12: initialize_zmq_socket
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
def initialize_zmq_socket(host, port):
logger.info("Initializing ZMQ consumer socket: Host: %s, Port: %d", host, port)
context = Context()
zmq_socket = context.socket(PULL)
zmq_socket.connect("tcp://{0}:{1}".format(host, port))
logger.info("ZMQ consumer socker initilized.")
return zmq_socket
示例13: Listener
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class Listener(Thread):
def __init__(self):
super(Listener, self).__init__(name="Listener")
self._shutdown = False
self.context = Context()
self.sub = self.context.socket(SUB)
self.sub.bind('tcp://*:7000')
self.sub.setsockopt(SUBSCRIBE, "")
self.poller = Poller()
self.poller.register(self.sub, POLLIN)
def cleanup(self):
self.sub.close()
self.context.term()
def run(self):
while True:
socks = dict(self.poller.poll(timeout=1))
if socks.get(self.sub) == POLLIN:
msg = self.sub.recv(flags=NOBLOCK)
print msg
if self._shutdown:
break
self.cleanup()
示例14: HomeBase
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
class HomeBase(Thread):
def __init__(self):
super(HomeBase, self).__init__(name="HomeBase")
self.context = Context()
self.pull = self.context.socket(PULL)
self.pull.bind("tcp://*:7001")
self._shutdown = False
self.poller = Poller()
self.poller.register(self.pull, POLLIN)
def cleanup(self):
self.pull.close()
self.context.term()
def run(self):
while True:
socks = dict(self.poller.poll(timeout=1))
if socks.get(self.pull) == POLLIN:
msg = self.pull.recv()
msg += ", WORK RECEIVED "
print msg
if self._shutdown:
break
self.cleanup()
示例15: init_connections
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import socket [as 别名]
def init_connections(self):
"""
Initialise zmq sockets, poller and queues.
Because this class is s Process, This method must be call in the run
method to be hold by the correct process.
"""
# Prepare our context and sockets
context = Context()
# Socket to talk to prev_stages
for name,connections in self.connections.items():
self.queue_limit[name] = connections[2]
sock_router = context.socket(ROUTER)
try:
sock_router.bind('tcp://*:' + connections[0])
except ZMQError as e:
self.log.error('{} : tcp://localhost:{}'
.format(e, connections[0]))
return False
self.router_sockets[name] = sock_router
# Socket to talk to next_stages
sock_dealer = context.socket(ROUTER)
try:
sock_dealer.bind("tcp://*:" + connections[1] )
except ZMQError as e:
self.log.error('{} : tcp://localhost:{}'
.format(e, connections[1]))
return False
self.dealer_sockets[name] = sock_dealer
self.next_available_stages[name] = list()
self.queue_jobs[name] = list()
# Use a ZMQ Pool to get multichannel message
self.poller = Poller()
# Register dealer socket to next_stage
for n, dealer in self.dealer_sockets.items():
self.poller.register(dealer, POLLIN)
for n, router in self.router_sockets.items():
self.poller.register(router, POLLIN)
# Register router socket to prev_stages or producer
self.socket_pub = context.socket(PUB)
if self.gui_address is not None:
try:
self.socket_pub.connect("tcp://" + self.gui_address)
except ZMQError as e:
self.log.error("".format(e, self.gui_address))
return False
# This flag stop this current process
return True