-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_communication_example.py
More file actions
63 lines (51 loc) · 1.72 KB
/
process_communication_example.py
File metadata and controls
63 lines (51 loc) · 1.72 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
import time
import multiprocessing
from multiprocessing import process
from multiprocessing.context import Process
from multiprocessing.connection import Listener, Client
logging.basicConfig(format='%(levelname)s - %(asctime)s: %(message)s', datefmt='%H:%M:%S', level=logging.DEBUG)
def proc(server='localhost', port=6000, password=b'password'):
name = process.current_process().name
logging.info(f'{name} started')
address = (server, port)
listener = Listener(address, authkey=password)
conn = listener.accept()
logging.info(f'{name}: connection from {listener.last_accepted}')
# Loop for input from the connected process
while True:
msg = conn.recv()
logging.info(f'{name} data in :{msg}')
if msg == 'quit':
conn.close()
break
listener.close()
logging.info(f'{name} finished')
def main():
name = process.current_process().name
logging.info(f'{name} started')
address = 'localhost'
port = 2823
password = b'password'
p = Process(target=proc, args=[address, port, password], daemon=True, name="Worker")
p.start()
logging.info(f'{name} waiting on the working..')
time.sleep(1)
# Connect to the process
dest = (address, port)
conn = Client(dest, authkey=password)
while True:
command = input('\r\nEnter a command or type quit:\n').strip()
logging.info(f'{name} command: {command}')
conn.send(command)
if command == 'quit':
break
if p.is_alive():
logging.info(f'{name} terminating worker')
conn.close()
time.sleep(1)
p.terminate()
p.join()
logging.info(f'{name} finished')
if __name__ == "__main__":
main()