day33 python之多執行緒

2022-08-25 10:57:22 字數 3648 閱讀 3274

1.多執行緒例項

#

import threading

#import time##

import

threading

import

time

class

mythread(threading.thread):

def__init__

(self,num):

threading.thread.

__init__

(self)

self.num =num

defrun(self):

print("

running on number:%s

"%self.num)

time.sleep(3)

if__name__ == '

__main__':

t1 = mythread(1)

t2 = mythread(2)

t1.start()

t2.start()

print("

ending")

#class mythread(threading.thread):

#def __init__(self, num):

#threading.thread.__init__(self)

#self.num = num##

def run(self): # 定義每個執行緒要執行的函式##

print("running on number:%s" % self.num)##

time.sleep(3)##

#if __name__ == '__main__':

#t1 = mythread(1)

#t2 = mythread(2)

#t1.start()

#t2.start()##

print("ending......")

2.join

#

import threading

#import time##

#def music():

#print("begin to listen %s"%time.ctime())

#time.sleep(3)

#print("stop to listen %s" % time.ctime())##

#def game():

#time.sleep(4)

#t3=threading.thread(target=music)

#t3.start()##

print("begin to play game %s"%time.ctime())

#time.sleep(5)

#print("stop to play game %s" % time.ctime())##

#if __name__ == '__main__':##

t1= threading.thread(target=music)##

t2 = threading.thread(target=game)##

t1.start()

#t2.start()##

t1.join()

#t2.join()##

print("ending")##

import threading

#from time import ctime,sleep

#import time##

def listenmusic(name):##

print ("begin listening to %s. %s" %(name,ctime()))

#sleep(3)

#print("end listening %s"%ctime())##

def recordblog(title):##

print ("begin recording the %s! %s" %(title,ctime()))

#sleep(5)

#print('end recording %s'%ctime())##

threads = ##

t1 = threading.thread(target=listenmusic,args=('水手',))

#t2 = threading.thread(target=recordblog,args=('python執行緒',))##

##if __name__ == '__main__':

##t1.setdaemon(true)

#t2.setdaemon(true)##

for t in threads:

##t.setdaemon(true) #注意:一定在start之前設定

#t.start()

#print(t.getname())

#print("count:",threading.active_count())

##t.join()#序列

##t.join()##

#t1.join()

##t1.setdaemon(true)##

#t2.join()########考慮這三種join位置下的結果?##

while threading.active_count()==1:##

print ("all over %s" %ctime())

#呼叫方式2:#######################################

#import threading

#import time

#import

threading

import

time

class

mythread(threading.thread):

def__init__

(self,num):

threading.thread.

__init__

(self)

self.num =num

defrun(self):

print("

running on number:%s

"%self.num)

time.sleep(3)

if__name__ == '

__main__':

t1 = mythread(1)

t2 = mythread(2)

t1.start()

t2.start()

print("

ending")

#class mythread(threading.thread):##

def __init__(self, num):

#threading.thread.__init__(self)

#self.num = num##

def run(self): # 定義每個執行緒要執行的函式##

print("running on number:%s" % self.num)##

time.sleep(3)##

if __name__ == '__main__':##

t1 = mythread(1)

#t2 = mythread(2)

#t1.start()

#t2.start()

#print("ending......")

Python之多執行緒

1 呼叫thread模組中的start new thread 函式來產生新執行緒 thread.start new thread function,args kwargs function 執行緒函式。args 傳遞給執行緒函式的引數,他必須是個tuple型別。kwargs 可選引數。2 使用thr...

python之多執行緒

學習了一下多執行緒 用到爬蟲裡面簡直爽歪歪呀 定義就很簡單,為了實現高併發,能夠同時在乙個指令碼下執行多個程式,節約時間 新增執行緒用到的 import threading as td def sum num1,num2 sum num1 num2 print sss sum def divided...

python之多執行緒

我們在商超買東西時,當只有乙個收銀台時,會導致排很長的隊。如果有多個收銀台同時工作的話,會大大提高效率。這是生活中的多執行緒,即多個執行緒同時工作。我們接下來用 案例講解多執行緒與單執行緒的區別。單執行緒即在程式執行過程中,按照一定的先後順序執行。多執行緒即多個事件同時發生。單執行緒 import ...