本文整理汇总了Python中zmq.Context.destroy方法的典型用法代码示例。如果您正苦于以下问题:Python Context.destroy方法的具体用法?Python Context.destroy怎么用?Python Context.destroy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq.Context
的用法示例。
在下文中一共展示了Context.destroy方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrapped_dispatcher
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [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()
示例2: WorkerTest
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [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()
示例3: run
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [as 别名]
def run(self):
context = Context()
socket = context.socket(self.socket_type)
socket.bind(self.socket_url)
data = socket.recv_pyobj()
self.queue.put(data)
context.destroy()
self.queue.close()
示例4: _request_translation
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [as 别名]
def _request_translation(self, language, country, key, plural):
""" Start up a worker, sync it and then send it a translation request.
Returns the result, shuts down the worker at the end as well.
Fails the current test, if something goes wrong.
"""
request = [
language, country if country is not None else "", key,
str(plural) if plural is not None else ""]
request = [x.encode(_ENCODING) for x in request]
context = Context()
# Create synchronization and backend sockets.
try:
sync_socket = context.socket(ROUTER)
try:
sync_socket.bind(_SYNC_ENDPOINT)
backend = context.socket(DEALER)
try:
backend.bind(_REQUEST_ENDPOINT)
worker_threads, worker_identities = _start_workers(
context, sync_socket, 1, _TIMEOUT)
poller = Poller()
poller.register(backend, POLLIN)
poller.register(sync_socket, POLLIN)
# Send request.
backend.send_multipart(
[worker_identities[0], b""] + request)
sockets = dict(poller.poll(_TIMEOUT))
# Shutdown worker.
_shut_down_workers(
sync_socket, worker_threads, worker_identities,
_TIMEOUT / 1000.0)
if backend in sockets:
# Return translation.
return backend.recv_multipart()[2].decode("utf-8")
self.fail("Worker did not response the request in time.")
finally:
backend.set(LINGER, 0)
backend.close()
finally:
sync_socket.set(LINGER, 0)
sync_socket.close()
finally:
context.destroy(linger=0)
示例5: _server
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [as 别名]
def _server(self, response):
""" Wait for a client request, record it and send the response. """
context = Context()
try:
socket = context.socket(REP)
try:
socket.set(LINGER, 0)
socket.bind("tcp://*:{}".format(_PORT))
poller = Poller()
poller.register(socket, POLLIN)
sockets = dict(poller.poll(_TIMEOUT))
if socket in sockets:
self._client_request = socket.recv_multipart()
if response:
socket.send_multipart(response)
finally:
socket.close()
finally:
context.destroy(linger=0)
示例6: Zero
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [as 别名]
class Zero(object):
def __init__(self):
self.context = Context()
@property
def subscriber(self):
try:
self._subscriber = self.context.socket(SUB)
self._subscriber.bind(ZERO_BIND_ADDRESS)
self._subscriber.setsockopt(SUBSCRIBE, "")
except ZMQError as error:
Logger.log('E200: {}'.format(error), 'CRITICAL')
exit(200)
except Exception as error:
Logger.log('E201: {}'.format(error), 'CRITICAL')
exit(201)
else:
Logger.log('bind subscriber on {}'.format(ZERO_BIND_ADDRESS), 'DEBUG')
return self._subscriber
@property
def publisher(self):
try:
self._publisher = self.context.socket(PUB)
self._publisher.connect(ZERO_CONNECT_ADDRESS)
except ZMQError as error:
Logger.log('E202: {}'.format(error), 'CRITICAL')
exit(202)
except Exception as error:
Logger.log('E203: {}'.format(error), 'CRITICAL')
exit(203)
else:
Logger.log('connection publisher on {}'.format(ZERO_CONNECT_ADDRESS), 'DEBUG')
return self._publisher
@property
def destroy(self):
self._subscriber.close()
self._publisher.close()
self.context.destroy()
Logger.log('destroy zmq socket', 'DEBUG')
示例7: dummy_onload
# 需要导入模块: from zmq import Context [as 别名]
# 或者: from zmq.Context import destroy [as 别名]
def dummy_onload():
context = Context()
socket = context.socket(PUSH)
socket.connect('tcp://127.0.0.1:5560')
socket.send_pyobj(True)
context.destroy()