python執行多執行緒的例項

2021-06-05 02:48:42 字數 1031 閱讀 8744

#-*- encoding: gb2312 -*-

import threading

import time

from socket import *

class mythread(threading.thread):

def __init__(self,host,port,msg,bf):

threading.thread.__init__(self)

self.host = host

self.port = port

self.msg = msg

self.bf = bf

def run(self):

print 'connect server and get the devipaddress '+str(self.msg)

port = int(self.port)

address=(self.host,port)

print address

clientsocket=socket(af_inet,sock_stream)

#建立採用tcp 協議的socket物件

clientsocket.connect(address)

#連線指定的伺服器端

while true:

time.sleep(3)

clientsocket.send(self.msg)

msg=clientsocket.recv(self.bf)

if (len(msg) > 0):

print msg

def test():

thread1 = mythread(『127.0.0.1』,7071,'aaa',1024)

thread2 = mythread(『127.0.0.1』,7071,'ccc',1025)

thread1.start() 

thread2.start() 

time.sleep(10)

if __name__ == '__main__': 

test() 

python執行多執行緒的例項

encoding gb2312 import threading import time from socket import class mythread threading.thread def init self,host,port,msg,bf threading.thread.init s...

python多執行緒例項

a 建立乙個thread的例項,傳給他乙個函式 import threading from time import sleep def thread body arg1,arg2 print i am a child thread arg1,arg2 sleep 10 print end child...

python多執行緒例項

import threading import time class mythread threading.thread def init self,n super mythread,self init self.n n def run self print runnint task self.n ...