python3 多線程的使用


示例1: 

import threading
from time import sleep

class forThread(threading.Thread):
def __init__(self, event):
threading.Thread.__init__(self)
self.name = '我的多線程'
self.event = event

def run(self):
print('你想讓程序干什么,在這里讓它幫你干就行了。。。')
if type(self.event) is int:
print('如果是個整數,就睡眠')
sleep(self.event)
print('睡眠個{}秒'.format(self.event))
elif type(self.event) is str:
print('如果是個人名,就直接打印出來')
print(self.event)
elif type(self.event) is dict:
print('如果是個字典,就先睡眠以字典長度的值的秒數,然后將它遍歷')
sleep(len(self.event))
for key in self.event:
print(key, ':', self.event[key])

if __name__ == '__main__':
eventList = [3, 'Tom', {'name': 'Lucy', 'age': 16, 'grade': 98, 'addr': '北京西站南廣場東', 'profession': '法師'}]
thread1 = forThread(eventList[0])
thread2 = forThread(eventList[1])
thread3 = forThread(eventList[2])

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

thread1.join()
thread2.join()
thread3.join()

結果:

你想讓程序干什么,在這里讓它幫你干就行了。。。
如果是個整數,就睡眠
你想讓程序干什么,在這里讓它幫你干就行了。。。
如果是個人名,就直接打印出來
Tom
你想讓程序干什么,在這里讓它幫你干就行了。。。
如果是個字典,就先睡眠以字典長度的值的秒數,然后將它遍歷
睡眠個3秒
name : Lucy
age : 16
grade : 98
addr : 北京西站南廣場東
profession : 法師

 點評:

使用過程比較麻煩,不適合大規模實際生產過程。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM