本文整理匯總了Python中zmq.eventloop.zmqstream.ZMQStream方法的典型用法代碼示例。如果您正苦於以下問題:Python zmqstream.ZMQStream方法的具體用法?Python zmqstream.ZMQStream怎麽用?Python zmqstream.ZMQStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zmq.eventloop.zmqstream
的用法示例。
在下文中一共展示了zmqstream.ZMQStream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def __init__(self, zmq_context, trade_in_socket, trade_pub = None, reopen=True):
self.zmq_context = zmq_context
self.connection_id = None
self.trade_in_socket = trade_in_socket
self.is_logged = False
self.user_id = None
self.reopen = reopen
self.trade_pub = trade_pub
self.trade_pub_socket = None
self.trade_pub_socket_stream = None
if self.trade_pub:
self.trade_pub_socket = self.zmq_context.socket(zmq.SUB)
self.trade_pub_socket.connect(self.trade_pub)
self.trade_pub_socket_stream = ZMQStream(self.trade_pub_socket)
self.trade_pub_socket_stream.on_recv(self._on_trade_publish)
示例2: connect
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def connect(self):
if not self.trade_pub_socket and self.trade_pub:
self.trade_pub_socket = self.zmq_context.socket(zmq.SUB)
self.trade_pub_socket.connect(self.trade_pub)
self.trade_pub_socket_stream = ZMQStream(self.trade_pub_socket)
self.trade_pub_socket_stream.on_recv(self._on_trade_publish)
self.trade_in_socket.send( "OPN," + base64.b32encode(os.urandom(10)))
response_message = self.trade_in_socket.recv()
opt_code = response_message[:3]
raw_message = response_message[4:]
if opt_code != 'OPN':
if opt_code == 'ERR':
raise TradeClientException( error_message = raw_message )
raise TradeClientException( error_message = 'Protocol Error: Unknown message opt_code received' )
self.connection_id = raw_message
示例3: register
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def register(self):
"""send the registration_request"""
self.log.info("Registering with controller at %s"%self.url)
ctx = self.context
connect,maybe_tunnel = self.init_connector()
reg = ctx.socket(zmq.DEALER)
reg.setsockopt(zmq.IDENTITY, self.bident)
connect(reg, self.url)
self.registrar = zmqstream.ZMQStream(reg, self.loop)
content = dict(uuid=self.ident)
self.registrar.on_recv(lambda msg: self.complete_registration(msg, connect, maybe_tunnel))
# print (self.session.key)
self.session.send(self.registrar, "registration_request", content=content)
示例4: __init__
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def __init__(self, queue):
self.queue = queue
self.io_loop = IOLoop(make_current=False)
context = zmq.Context.instance()
self.pause_socket = context.socket(zmq.REP)
self.pause_socket_port = self.pause_socket.bind_to_random_port(
"tcp://127.0.0.1")
self.status_socket = context.socket(zmq.PULL)
self.status_socket_port = self.status_socket.bind_to_random_port(
"tcp://127.0.0.1")
self.pause_stream = ZMQStream(self.pause_socket, self.io_loop)
self.status_stream = ZMQStream(self.status_socket, self.io_loop)
self.pause_stream.on_recv(self.recv_pause)
self.status_stream.on_recv(self.recv_status)
self.thread = threading.Thread(target=self._thread_main)
self.thread.daemon = True
示例5: init_networking
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def init_networking(self):
"""
Creates socket, connects to specified port on localhost,
and starts the :meth:`~Net_Node.threaded_loop` as a daemon thread.
"""
self.sock = self.context.socket(zmq.DEALER)
self.sock.identity = self.id
#self.sock.probe_router = 1
# net nodes are local only
self.sock.connect('tcp://localhost:{}'.format(self.port))
# wrap in zmqstreams and start loop thread
self.sock = ZMQStream(self.sock, self.loop)
self.sock.on_recv(self.handle_listen)
self.loop_thread = threading.Thread(target=self.threaded_loop)
self.loop_thread.daemon = True
self.loop_thread.start()
self.repeat_thread = threading.Thread(target=self.repeat)
self.repeat_thread.daemon = True
self.repeat_thread.start()
#self.connected = True
示例6: start
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def start(self):
"""Start ZAP authentication"""
super(IOLoopAuthenticator, self).start()
self.zap_stream = zmqstream.ZMQStream(self.zap_socket, self.io_loop)
self.zap_stream.on_recv(self.handle_zap_message)
示例7: setUp
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def setUp(self):
try:
from tornado import ioloop
except ImportError:
pytest.skip("Requires tornado")
from zmq.eventloop import zmqstream
self.fail_msg = None
self.io_loop = ioloop.IOLoop()
super(TestIOLoopAuthentication, self).setUp()
self.server = self.socket(zmq.PUSH)
self.client = self.socket(zmq.PULL)
self.pushstream = zmqstream.ZMQStream(self.server, self.io_loop)
self.pullstream = zmqstream.ZMQStream(self.client, self.io_loop)
示例8: setUp
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def setUp(self):
if tornado is None:
pytest.skip()
self.context = zmq.Context()
self.loop = ioloop.IOLoop.instance()
self.push = zmqstream.ZMQStream(self.context.socket(zmq.PUSH))
self.pull = zmqstream.ZMQStream(self.context.socket(zmq.PULL))
port = self.push.bind_to_random_port('tcp://127.0.0.1')
self.pull.connect('tcp://127.0.0.1:%i' % port)
self.stream = self.push
示例9: subscribe
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def subscribe(self,zmq_context,trade_pub_connection_string,trade_client):
"""" subscribe. """
self.md_pub_socket = zmq_context.socket(zmq.SUB)
self.md_pub_socket.connect(trade_pub_connection_string)
self.md_pub_socket.setsockopt(zmq.SUBSCRIBE,"^MD_FULL_REFRESH_" +self.symbol + '$')
self.md_pub_socket.setsockopt(zmq.SUBSCRIBE,"^MD_TRADE_" + self.symbol + '$')
self.md_pub_socket.setsockopt(zmq.SUBSCRIBE,"^MD_INCREMENTAL_" +self.symbol +".0$")
self.md_pub_socket.setsockopt(zmq.SUBSCRIBE,"^MD_INCREMENTAL_" +self.symbol +".1$")
self.md_pub_socket_stream = ZMQStream(self.md_pub_socket)
self.md_pub_socket_stream.on_recv(self.on_md_publish)
md_subscription_msg = {
'MsgType': 'V',
'MDReqID': '0', # not important.
'SubscriptionRequestType': '0',
'MarketDepth': 0,
'TradeDate': time.strftime("%Y%m%d", time.localtime()),
'MDUpdateType': '0',
'MDEntryTypes': ['0', '1', '2'],
'Instruments': [self.symbol]
}
self.application.log('DEBUG', 'MARKET_DATA_SUBSCRIBER', 'SUBSCRIBE' )
return trade_client.sendJSON(md_subscription_msg)
示例10: __init__
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def __init__(self, **kwargs):
super(LogWatcher, self).__init__(**kwargs)
s = self.context.socket(zmq.SUB)
s.bind(self.url)
self.stream = zmqstream.ZMQStream(s, self.loop)
self.subscribe()
self.on_trait_change(self.subscribe, 'topics')
示例11: as_zmqstream
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def as_zmqstream(f):
def wrapped(self, *args, **kwargs):
socket = f(self, *args, **kwargs)
return ZMQStream(socket, self.loop)
return wrapped
示例12: test_tracking
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def test_tracking(self):
"""test tracking messages"""
a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
s = self.session
s.copy_threshold = 1
stream = ZMQStream(a)
msg = s.send(a, 'hello', track=False)
self.assertTrue(msg['tracker'] is ss.DONE)
msg = s.send(a, 'hello', track=True)
self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker))
M = zmq.Message(b'hi there', track=True)
msg = s.send(a, 'hello', buffers=[M], track=True)
t = msg['tracker']
self.assertTrue(isinstance(t, zmq.MessageTracker))
self.assertRaises(zmq.NotDone, t.wait, .1)
del M
t.wait(1) # this will raise
# def test_rekey(self):
# """rekeying dict around json str keys"""
# d = {'0': uuid.uuid4(), 0:uuid.uuid4()}
# self.assertRaises(KeyError, ss.rekey, d)
#
# d = {'0': uuid.uuid4(), 1:uuid.uuid4(), 'asdf':uuid.uuid4()}
# d2 = {0:d['0'],1:d[1],'asdf':d['asdf']}
# rd = ss.rekey(d)
# self.assertEqual(d2,rd)
#
# d = {'1.5':uuid.uuid4(),'1':uuid.uuid4()}
# d2 = {1.5:d['1.5'],1:d['1']}
# rd = ss.rekey(d)
# self.assertEqual(d2,rd)
#
# d = {'1.0':uuid.uuid4(),'1':uuid.uuid4()}
# self.assertRaises(KeyError, ss.rekey, d)
#
示例13: send_raw
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def send_raw(self, stream, msg_list, flags=0, copy=True, ident=None):
"""Send a raw message via ident path.
This method is used to send a already serialized message.
Parameters
----------
stream : ZMQStream or Socket
The ZMQ stream or socket to use for sending the message.
msg_list : list
The serialized list of messages to send. This only includes the
[p_header,p_parent,p_metadata,p_content,buffer1,buffer2,...] portion of
the message.
ident : ident or list
A single ident or a list of idents to use in sending.
"""
to_send = []
if isinstance(ident, bytes):
ident = [ident]
if ident is not None:
to_send.extend(ident)
to_send.append(DELIM)
to_send.append(self.sign(msg_list))
to_send.extend(msg_list)
stream.send_multipart(to_send, flags, copy=copy)
示例14: recv
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def recv(self, socket, mode=zmq.NOBLOCK, content=True, copy=True):
"""Receive and unpack a message.
Parameters
----------
socket : ZMQStream or Socket
The socket or stream to use in receiving.
Returns
-------
[idents], msg
[idents] is a list of idents and msg is a nested message dict of
same format as self.msg returns.
"""
if isinstance(socket, ZMQStream):
socket = socket.socket
try:
msg_list = socket.recv_multipart(mode, copy=copy)
except zmq.ZMQError as e:
if e.errno == zmq.EAGAIN:
# We can convert EAGAIN to None as we know in this case
# recv_multipart won't return None.
return None,None
else:
raise
# split multipart message into identity list and message dict
# invalid large messages can cause very expensive string comparisons
idents, msg_list = self.feed_identities(msg_list, copy)
try:
return idents, self.unserialize(msg_list, content=content, copy=copy)
except Exception as e:
# TODO: handle it
raise e
示例15: run
# 需要導入模塊: from zmq.eventloop import zmqstream [as 別名]
# 或者: from zmq.eventloop.zmqstream import ZMQStream [as 別名]
def run(self):
"""The thread's main activity. Call start() instead."""
self.socket = self.context.socket(zmq.DEALER)
self.socket.setsockopt(zmq.IDENTITY, self.session.bsession)
self.socket.connect(self.address)
self.stream = zmqstream.ZMQStream(self.socket, self.ioloop)
self.stream.on_recv(self._handle_recv)
self._run_loop()
try:
self.socket.close()
except:
pass