Python进阶13---并发和线程
并发
基本概念
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。并发和并行的区别
并发的解决
进程和线程
线程的状态
Python中的进程和线程
Python的线程开发
Thread类
线程启动
import threading import time #最简单的线程程序 def worker(): for _ in range(5): time.sleep(0.5) print('welcome ') print('Thread over') t = threading.Thread(target=worker)#线程对象 t.start()#启动
import threading import time #最简单的线程程序 def worker(): while True: time.sleep(0.5) print("I'm working") print('Thread over') t = threading.Thread(target=worker)#线程对象 t.start()#启动
线程退出
import threading import time def worker(): count = 0 while True: if (count>5): raise RuntimeError(count) time.sleep(1) print("I'm working") count += 1 t = threading.Thread(target=worker)#线程对象 t.start()#启动 print('===End===')
线程的传参
import threading import time def add(x,y): print(threading.current_thread()) print('{}+{}={} {}'.format(x,y,x+y,threading.current_thread().ident)) thread1 = threading.Thread(target=add,name='add1',args=(4,5))#线程对象 thread1.start() time.sleep(2) thread2 = threading.Thread(target=add,name='add2',args=(5,),kwargs={'y':4})#线程对象 thread2.start() time.sleep(2) thread3 = threading.Thread(target=add,name='add3',kwargs={'x':4,'y':5}) thread3.start()
threading的属性和方法
#示例1 import threading import time def worker(n=5): print("in",threading.current_thread()) print(threading.main_thread()) print(threading.active_count()) print(threading.enumerate()) for _ in range(n): time.sleep(0.5) print('welcome') print('Thread over') print("out",threading.current_thread()) t = threading.Thread(target=worker,name='w1') t.start() # t1 = threading.Thread(target=worker,name='w2') # t1.start() print(threading.enumerate()) #[<_MainThread(MainThread, started 18880)>, <Thread(w1, started 7404)>]或者 # [<_MainThread(MainThread, stopped 18720)>, <Thread(w1, started 22448)>]
#示例2 import threading import time def showthreadinfo(n=5): print("currentthread ={}".format(threading.current_thread())) print("main thread = {}".format(threading.main_thread())) print("active count ={}".format(threading.active_count())) print(threading.enumerate()) def worker(): count = 0 showthreadinfo() while True: if count>5: break time.sleep(0.5) count += 1 print("I'm working") t = threading.Thread(target=worker,name='worker')#线程对象 showthreadinfo() t.start() #此时输出如下: currentthread =<_MainThread(MainThread, started 18900)> main thread = <_MainThread(MainThread, started 18900)> active count =1 [<_MainThread(MainThread, started 18900)>] currentthread =<Thread(worker, started 19512)> main thread = <_MainThread(MainThread, stopped 18900)> active count =2 [<_MainThread(MainThread, stopped 18900)>, <Thread(worker, started 19512)>]
...
Thread实例的属性和方法
import threading import time def worker(): count = 0 while True: if count>5: break time.sleep(0.5) count += 1 print(threading.current_thread().name) t = threading.Thread(target=worker,name='worker')#线程对象 print(11,t.ident)#输出None,因为还未启动该线程 t.start() while True: time.sleep(1) if t.is_alive(): print('{} {} alive'.format(t.name,t.ident)) else: print('{} {} dead'.format(t.name,t.ident)) t.start()#可以否?#不可以,RuntimeError: threads can only be started once
start方法
import threading import time class MyThread(threading.Thread): def run(self): print('run') super().run() def start(self): print('start') return super().start() def worker(): count = 0 while True: if count>2: break time.sleep(0.5) count += 1 print('working') print('Thread over') t = MyThread(target=worker,name='w1') t.start() #输出如下: start run working working working Thread over
run方法
import threading import time class MyThread(threading.Thread): def run(self): print('run') super().run() def start(self): print('start') return super().start() def worker(): count = 0 while True: if count>2: break time.sleep(0.5) count += 1 print('working') print('Thread over') t = MyThread(target=worker,name='w1') t.run() #输出如下: run working working working Thread over
start和run的区别
import threading import time class MyThread(threading.Thread): def run(self): print('run') super().run() def start(self): print('start') return super().start() def worker(): count = 0 while True: if count>2: break time.sleep(0.5) count += 1 print('working') print('Thread over') print(threading.current_thread().name) t = MyThread(target=worker,name='w1') # t.run()#输出MainThread t.start()#输出w1
多线程
import threading import time def worker(): count = 0 while True: if count>5: break time.sleep(0.5) count += 1 print('working') print('Thread over') print(threading.current_thread().name,threading.current_thread().ident) class MyThread(threading.Thread): def run(self): print('run') super().run() def start(self): print('start') return super().start() t1 = MyThread(target=worker,name='worker1') t2 = MyThread(target=worker,name='worker2') t1.start() t2.start() # t1.run() # t2.run()
待续。

更多精彩