-
Notifications
You must be signed in to change notification settings - Fork 1
/
FASLaunchControlOutput.py
79 lines (60 loc) · 2.32 KB
/
FASLaunchControlOutput.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# reference: https://qpython.readthedocs.io/en/latest/usage-examples.html#asynchronous-query
import random
import threading
import time
from qpython import qconnection
from qpython.qtype import QException
from qpython.qconnection import MessageType
from qpython.qcollection import QDictionary
class ListenerThread(threading.Thread):
def __init__(self, q):
super(ListenerThread, self).__init__()
self.q = q
self._stopper = threading.Event()
def stop(self):
self._stopper.set()
def stopped(self):
return self._stopper.isSet()
def run(self):
while not self.stopped():
print('.')
try:
# retrieve entire message
message = self.q.receive(data_only=False, raw=False)
if message.type != MessageType.ASYNC:
print('Unexpected message, expected message of type: ASYNC')
print('type: %s, message type: %s, data size: %s, is_compressed: %s ' % (
type(message), message.type, message.size, message.is_compressed))
print(message.data)
if isinstance(message.data, QDictionary):
# stop after 10th query
if message.data[b'queryid'] == 9:
self.stop()
except QException as e:
print(e)
if __name__ == '__main__':
# create connection object
q = qconnection.QConnection(
host='renxiang.cloud', port=5001, username='foorx', password='foorxaccess')
# initialize connection
q.open()
print(q)
print('IPC version: %s. Is connected: %s' %
(q.protocol_version, q.is_connected()))
try:
# definition of asynchronous multiply function
# queryid - unique identifier of function call - used to identify
# the result
# a, b - parameters to the query
q.sendSync(
' :{[queryid;a;b] res:a*b; (neg .z.w)(`queryid`result!(queryid;res)) }')
t = ListenerThread(q)
t.start()
for x in range(10):
a = random.randint(1, 100)
b = random.randint(1, 100)
print('Asynchronous call with queryid=%s with arguments: %s, %s' % (x, a, b))
q.sendAsync('asynchMult', x, a, b)
time.sleep(1)
finally:
q.close()