python中程序的多线程并行执行

时间:2018-04-20 10:24:03

标签: python multithreading

server.py

import socket
sock = socket.socket(socket.AF_INET ,socket.SOCK_STREAM)
print "socket success"

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sock.bind(('127.0.0.1',9999))

print "binded success fully"

sock.listen(5)

while True :
     print "waiting for connection"
     conn,addr = sock.accept()
     while True :
         print conn.recv(30)
         data = "Ack for msg1 client sender 1"
         conn.send(data)
         print "data send success"
         print "waiting for responce comming from client sender2"
         print conn.recv(30)
         print "i am receving from client sender3"
         print conn.recv(30)

     conn.close()
sock.close()

client.py

import threading
import socket

sock = socket.socket(socket.AF_INET ,socket.SOCK_STREAM)

print "client socket connection success"


host = '127.0.0.1'
port = 9999

sock.connect((host , port))

print "socket conncection is established"

def fun1(msg ,num) :
        print "msg is ",msg
        for index in range(num) :
                sock.send(msg)
                print "data sent success",index

def fun2(msg ,num) :
        print "msg is ",msg
        for index in range(num) :
                print sock.recv(20)
                print "data rxved successfully"
def fun3(msg ,num) :
        print "msg is ",msg
        for index in range(num) :
                sock.send(msg)
                print "data sent success",index


thread1 = threading.Thread(target=fun1("i am thread1",5))
thread2 = threading.Thread(target=fun2("i am thread2",5))
thread3 = threading.Thread(target=fun3("i am thread3",5))

thread1.start()
thread2.start()
thread3.start()

客户端输出

client socket connection success
socket conncection is established
client socket connection success
socket conncection is established
msg is  i am thread1
data sent success 0
data sent success 1
data sent success 2
data sent success 3
data sent success 4
msg is  i am thread2
Ack for ms
data rxved successfully
g1 client 
data rxved successfully
sender 1
data rxved successfully
Ack for ms
data rxved successfully
g1 client 
data rxved successfully
msg is  i am thread3
data sent success 0
data sent success 1
data sent success 2
data sent success 3
data sent success 4
data sent success 1
data sent success 2
data sent success 3
data sent success 4
msg is  i am thread2
Ack for msg client sender 1
data rxved successfully
g1 client 
data rxved successfully
sender 1
data rxved successfully
Ack for ms
data rxved successfully
g1 client 
data rxved successfully
msg is  i am thread3
data sent success 0
data sent success 1
data sent success 2
data sent success 3
data sent success 4

其实我想输出是这样的:

client socket connection success
socket conncection is established
msg is  i am thread1
data sent success 0
i am thread2
Ack for msg 1 client sender 1
msg is  i am thread3
data sent success 0

msg is  i am thread1
data sent success 2
i am thread2
Ack for msg 1 client sender 1
msg is  i am thread3
data sent success 2
etc ....

All threads start at the same time but i was failing to achieve the output like this. please any one help on this .All threads has to execute once but i need solution without using any sleep mechanism 

在一次迭代之后,rad1必须将日期发送给客户端                            thread2必须从服务器接收数据                            thread3再次根据thread2结果发送数据。 thread3依赖于thread2输出.output看起来像一个线性但我需要更快的执行。

0 个答案:

没有答案