-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathclient.py
More file actions
39 lines (30 loc) · 1.03 KB
/
client.py
File metadata and controls
39 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from msgpackrpc import Loop
from msgpackrpc import session
from msgpackrpc.transport import tcp
class Client(session.Session):
"""\
Client is useful for MessagePack RPC API.
"""
def __init__(self, address, timeout=10, loop=None, builder=tcp, reconnect_limit=5):
loop = loop or Loop()
session.Session.__init__(self, address, timeout, loop, builder, reconnect_limit)
if timeout:
loop.attach_periodic_callback(self.step_timeout, 1000) # each 1s
@classmethod
def open(cls, *args):
assert cls is Client, "should only be called on sub-classes"
client = Client(*args)
return Client.Context(client)
class Context(object):
"""\
For with statement
"""
def __init__(self, client):
self._client = client
def __enter__(self):
return self._client
def __exit__(self, type, value, traceback):
self._client.close()
if type:
return False
return True