-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.py
More file actions
85 lines (74 loc) · 2.08 KB
/
socket.py
File metadata and controls
85 lines (74 loc) · 2.08 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#Создать процесс с двумя дочерними, процесс с двумя потоками.
#Организовать передачу данных между ними.
import multiprocessing
import time
import threading
import socket
#suprocess 1
def fun1():
print('Child Process 1 started')
#client
sock=socket.socket()
sock.connect(('localhost',64444))
sdata='Subprocess 1 send message to Main Process'
sock.send(sdata.encode("utf-8"))
data=sock.recv(1024)
print(data.decode("utf-8"))
sock.close()
#subprocess 2
def fun2():
print('Child Process 2 started')
#client
sock=socket.socket()
sock.connect(('localhost',64444))
sdata='Subprocess 2 send message to Main Process'
sock.send(sdata.encode("utf-8"))
sock.close()
#main process 2
def fun3():
print('Process 2 started')
t1=threading.Thread(target=fun4)
t2=threading.Thread(target=fun4)
t1.start()
t2.start()
#Threads
def fun4():
print('Thread started')
#client
sock=socket.socket()
sock.connect(('localhost',64444))
sdata='Thread send message to Main Process'
sock.send(sdata.encode("utf-8"))
sock.close()
#main process 1/server
if __name__ =='__main__':
print('Process 1 started')
p1=multiprocessing.Process(target=fun1)
p2=multiprocessing.Process(target=fun2)
p1.start()
p2.start()
p3=multiprocessing.Process(target=fun3)
p3.start()
#server
sockserv=socket.socket()
sockserv.bind(('',64444))
sockserv.listen(5)
try:
while True:
conn, addr=sockserv.accept()
print ("New connection from " + addr[0])
data=conn.recv(1024)
if not data:
break
print(data.decode("utf-8"))
sdata=("accept")
conn.send(sdata.encode("utf-8"))
except:
print("Error")
finally:
conn.close()
sockserv.close()
p1.join()
p2.join()
p3.join()
time.sleep(10)