forked from capnproto/pycapnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_client.py
executable file
·59 lines (40 loc) · 1.4 KB
/
thread_client.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import argparse
import threading
import time
import capnp
import thread_capnp
capnp.remove_event_loop()
capnp.create_event_loop(threaded=True)
def parse_args():
parser = argparse.ArgumentParser(
usage="Connects to the Example thread server \
at the given address and does some RPCs"
)
parser.add_argument("host", help="HOST:PORT")
return parser.parse_args()
class StatusSubscriber(thread_capnp.Example.StatusSubscriber.Server):
"""An implementation of the StatusSubscriber interface"""
def status(self, value, **kwargs):
print("status: {}".format(time.time()))
def start_status_thread(host):
client = capnp.TwoPartyClient(host)
cap = client.bootstrap().cast_as(thread_capnp.Example)
subscriber = StatusSubscriber()
promise = cap.subscribeStatus(subscriber)
promise.wait()
def main(host):
client = capnp.TwoPartyClient(host)
cap = client.bootstrap().cast_as(thread_capnp.Example)
status_thread = threading.Thread(target=start_status_thread, args=(host,))
status_thread.daemon = True
status_thread.start()
print("main: {}".format(time.time()))
cap.longRunning().wait()
print("main: {}".format(time.time()))
cap.longRunning().wait()
print("main: {}".format(time.time()))
cap.longRunning().wait()
print("main: {}".format(time.time()))
if __name__ == "__main__":
main(parse_args().host)