Thread类 t = threading.Thread(target=None,name=None,args=(),kwargs=None,*,daemin=None)
target:线程调用的对象,目标函数 name:线程的名字,可以重名 args:目标函数传参,元祖 kwargs:传递关键字参数,字典
import threading import time def worker(): for _ in range(3): time.sleep(0.5) print("welcome mykernel") print("therad over") def worker1(): for _ in range(3): time.sleep(0.5) print("welcome mykernel......") print("therad over ..........") t = threading.Thread(target=worker,name='w1') t1 = threading.Thread(target=worker1,name='w2') t.start() t1.start()
welcome mykernel therad over welcome mykernel...... therad over .......... welcome mykernel therad over welcome mykernel...... therad over .......... welcome mykernel therad over welcome mykernel...... therad over ..........
python线程的没有特定的退出方法, 常见的退出:
import threading import time def worker(): count = 0 for _ in range(10): time.sleep(0.5) print("welcome mykernel") count +=1 if count >4: raise Exception("count大于4异常") t = threading.Thread(target=worker,name='w1') t.start()
welcome mykernel welcome mykernel welcome mykernel welcome mykernel welcome mykernel Exception in thread w1: Traceback (most recent call last): File "/root/.pyenv/versions/3.7.3/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/root/.pyenv/versions/3.7.3/lib/python3.7/threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "<ipython-input-9-0fc2f50f7f96>", line 11, in worker raise Exception("count大于4异常") Exception: count大于4异常
Python中的线程无优先级和线程组概念,不能被销毁、停止、挂起和恢复。
import threading import time def worker(n): count = 0 for _ in range(n): time.sleep(0.5) print("welcome mykernel") count +=1 if count >4: raise Exception("count大于4异常") t = threading.Thread(target=worker,name='w1',args=(5,)) t.start()
welcome mykernel welcome mykernel welcome mykernel welcome mykernel welcome mykernel Exception in thread w1: Traceback (most recent call last): File "/root/.pyenv/versions/3.7.3/lib/python3.7/threading.py", line 917, in _bootstrap_inner self.run() File "/root/.pyenv/versions/3.7.3/lib/python3.7/threading.py", line 865, in run self._target(*self._args, **self._kwargs) File "<ipython-input-4-36a6bbbdc89d>", line 11, in worker raise Exception("count大于4异常") Exception: count大于4异常
current_thread():返回当前线程对象 main_thread():返回主线程对象 active_thread():当前处于alive状态的线程个数 enumerate():返回所有活着的线程的列表,不包含已经终止和未开始的线程 get_ident():返回当前线程id飞0整数
import threading import time def worker(n): # 打印当前的线程 print(threading.current_thread()) # 打印该线程的主进程 print(threading.main_thread()) # 打印活着的进程数 print(threading.active_count()) # 打印活着进程的列表 print(threading.enumerate()) for _ in range(n): time.sleep(0.5) print("welcome mykernel") print(threading.current_thread()) t = threading.Thread(target=worker,name='w1',args=(5,)) t.start() print("T....",threading.enumerate(),'/n')
<_MainThread(MainThread, started 140182288873280)> <Thread(w1, started 140181357197056)> <_MainThread(MainThread, started 140182288873280)> 6 [<_MainThread(MainThread, started 140182288873280)>, <Thread(Thread-2, started daemon 140181894067968)>, <Heartbeat(Thread-3, started daemon 140181885675264)>, <HistorySavingThread(IPythonHistorySavingThread, started 140181860497152)>, <ParentPollerUnix(Thread-1, started daemon 140181851318016)>, <Thread(w1, started 140181357197056)>] T.... [<_MainThread(MainThread, started 140182288873280)>, <Thread(Thread-2, started daemon 140181894067968)>, <Heartbeat(Thread-3, started daemon 140181885675264)>, <HistorySavingThread(IPythonHistorySavingThread, started 140181860497152)>, <ParentPollerUnix(Thread-1, started daemon 140181851318016)>, <Thread(w1, started 140181357197056)>] /n welcome mykernel welcome mykernel welcome mykernel welcome mykernel welcome mykernel
name :名字标识,可以重名,使用getName(),setName()获取和设置 ident:线程id,是非0 整数,线程启动后才有id,否则为None,线程退出,此id依然可以访问,id可以重复使用 is_alive() 返回线程是否活着
import threading import time def worker(): count = 0 while True: if (count>4): break time.sleep(0.5) count +=1 print(threading.current_thread().name) print(threading.current_thread().ident) t = threading.Thread(name='worker',target=worker()) t.start() for _ in range(3): time.sleep(1) if t.is_alive(): print('{} {} alive'.format(t.name,t.ident)) else: print('{} {} dead'.format(t.name, t.ident))
MainThread 139942493099840 MainThread 139942493099840 MainThread 139942493099840 MainThread 139942493099840 MainThread 139942493099840 worker 139941778028288 dead worker 139941778028288 dead worker 139941778028288 dead
start() 启动线程,每一个线程必须且只能执行一次该方法 run() 运行线程函数
本文作者:mykernel
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!