-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_writer.py
60 lines (50 loc) · 1.45 KB
/
http_writer.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
import multiprocessing
import time
import urllib.request
import lib104
def write(command):
# req = urllib.request.Request("http://192.168.56.1:8081/write.json?tag=Devices.dev1.I1&val=777")
req = urllib.request.Request("http://192.168.56.1:8081/write.json?tag=Devices.dev1.Open&val=1")
res = urllib.request.urlopen(req)
return res.read()
def handler(e, v, inputs):
for input in inputs:
print("Wait for event: Starting", input)
time.sleep(1)
print("Writing")
print("Test Value now ---- ", id(v.value), v)
write("Hello")
e.wait()
if e.is_set():
print("Event catching!")
print("Test Value now(2) ---- ", v.value)
e.clear()
def worker(event, value):
client = lib104.Client("192.168.56.1", 2404, event, value)
time.sleep(2)
client.run()
while client.b_run:
time.sleep(1)
print("Client is running")
# event.set()
print("finish")
def main():
print("Main")
inputs = list(range(10))
e = multiprocessing.Event()
v = multiprocessing.Value('i', 0)
http_sender_thread = multiprocessing.Process(
name="waiter",
target=handler,
args=(e, v, inputs),
)
client_thread = multiprocessing.Process(
name="Client",
target=worker,
args=(e, v,),
)
http_sender_thread.start()
client_thread.start()
client_thread.join()
if __name__ == "__main__":
main()