8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
|
13 """PyAMS_zmq.socket module |
|
14 |
|
15 This module provides a few helpers which can be used to open a 0MQ socket and handle response. |
|
16 """ |
|
17 |
|
18 import zmq |
|
19 |
|
20 |
13 __docformat__ = 'restructuredtext' |
21 __docformat__ = 'restructuredtext' |
14 |
22 |
15 |
23 |
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 |
|
20 # import packages |
|
21 import zmq |
|
22 |
|
23 |
|
24 def zmq_socket(address, socket_type=zmq.REQ, linger=0, protocol='tcp', auth=None): |
24 def zmq_socket(address, socket_type=zmq.REQ, linger=0, protocol='tcp', auth=None): |
|
25 # pylint: disable=no-member |
25 """Get ØMQ socket |
26 """Get ØMQ socket |
26 |
27 |
27 auth is given as unicode 'username:password' string and automatically converted to bytes. |
28 auth is given as unicode 'username:password' string and automatically converted to bytes. |
28 """ |
29 """ |
29 context = zmq.Context() |
30 context = zmq.Context() |
39 """Get response from given socket""" |
40 """Get response from given socket""" |
40 poller = zmq.Poller() |
41 poller = zmq.Poller() |
41 poller.register(socket, flags) |
42 poller.register(socket, flags) |
42 if poller.poll(timeout * 1000): |
43 if poller.poll(timeout * 1000): |
43 return socket.recv_json() |
44 return socket.recv_json() |
44 else: |
45 return [503, "Connection timeout"] |
45 return [503, "Connection timeout"] |
|