自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python多線程編程初體驗

開發(fā) 后端
這將是一個系列,一個關(guān)于進程、線程和 協(xié)程的系列。主要用于:回顧和復(fù)習(xí)以往所學(xué)的知識 以及 希望這點經(jīng)驗?zāi)軌驇椭趯W(xué)習(xí)編程的你

 前言

這將是一個系列,一個關(guān)于進程、線程和 協(xié)程的系列。

主要用于:回顧和復(fù)習(xí)以往所學(xué)的知識 以及 希望這點經(jīng)驗?zāi)軌驇椭趯W(xué)習(xí)編程的你

[[417145]]

查看線程ID

創(chuàng)建文件 0809.py

 

  1. import time 
  2. import threading 
  3.  
  4. def loop(): 
  5.     while True
  6.         print('thread id is {}'.format(threading.get_native_id())) 
  7.         time.sleep(3) 
  8.          
  9. if __name__ == '__main__'
  10.     loop() 

 

在第一個終端窗口中執(zhí)行

 

  1. $ python 0809.py  
  2. thread id is 3344 
  3. thread id is 3344 
  4. thread id is 3344 
  5. ······ 

 

在第二個終端窗口中執(zhí)行

 

  1. ps -ef | grep 'python 0809.py' 
  2. vagrant   3344  3117  0 16:26 pts/1    00:00:00 python 0809.py 
  3. vagrant   3662  3451  0 16:30 pts/0    00:00:00 grep --color=auto python 0809.py 

 

你會發(fā)現(xiàn)其進程ID也是 3344和線程ID一致。這是因為Linux中規(guī)定,當(dāng)一個進程中只有一個線程的情況下,線程ID等于進程ID?;騽t說,進程的第一個線程(主線程)的ID等于進程ID。

經(jīng)典的生產(chǎn)者/消費者模型(也有人稱之為,發(fā)布/訂閱模型)

 

  1. # 0809.py  
  2. import time 
  3. import threading 
  4.  
  5. count = 0 
  6.  
  7. def consumer(): 
  8.     global count 
  9.     while True
  10.         if count <= 0: 
  11.             continue 
  12.         count = count - 1 
  13.         print(f'count is {count}, consumer thread id is {threading.get_native_id()}'
  14.         time.sleep(2) 
  15.  
  16. def producer(): 
  17.     global count 
  18.     while True
  19.         count = count + 1 
  20.         print(f'count is {count}, producer thread id is {threading.get_native_id()}'
  21.         time.sleep(1) 
  22.          
  23. if __name__ == '__main__'
  24.     tp = threading.Thread(target=producer) 
  25.     tc = threading.Thread(target=consumer) 
  26.     tp.start() 
  27.     tc.start() 

 

執(zhí)行命令 python 0809.py

  1. $ python 0809.py  
  2. count is 1, producer thread id is 3785 
  3. count is 0, consumer thread id is 3786 
  4. count is 1, producer thread id is 3785 
  5. count is 0, consumer thread id is 3786 
  6. count is 1, producer thread id is 3785 
  7. count is 2, producer thread id is 3785 
  8. count is 1, consumer thread id is 3786 
  9. count is 2, producer thread id is 3785 

可以發(fā)現(xiàn),兩個線程并非嚴格交替執(zhí)行,而是隨機執(zhí)行。

我們再來查看一下相關(guān)的進程和線程

 

  1. $ ps -ef | grep 'python 0809.py' 
  2. vagrant   3784  3117  0 17:24 pts/1    00:00:00 python 0809.py 
  3. vagrant   3789  3451  0 17:24 pts/0    00:00:00 grep --color=auto python 0809.py 
  4.  
  5. $ ps -T -p 3784 
  6.   PID  SPID TTY          TIME CMD 
  7.  3784  3784 pts/1    00:00:00 python 
  8.  3784  3785 pts/1    00:00:00 python 
  9.  3784  3786 pts/1    00:00:00 python 

 

可以看出該進程中有三個線程,分別是主線程 3784 和兩個子線程 3785(producer)、3786(consumer)

今天我們就先講到這里,重點掌握:

1、如何在python代碼中和shell終端中查看線程id 進程ID 以及進程中包含的線程。

2、理解生產(chǎn)/消費者模型,因為這個模型會在接下來的學(xué)習(xí)中被多次提到

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2011-06-07 17:35:39

iphone 多線程

2011-08-02 10:26:59

iOS 多線程 線程

2011-06-24 11:03:31

Qt 多線程 線程

2023-10-06 23:06:01

多線程Python

2011-06-20 13:23:03

Qt Quick QML

2013-07-16 10:12:14

iOS多線程多線程概念多線程入門

2023-06-13 13:39:00

多線程異步編程

2009-08-01 09:06:35

UbuntuOneLinux開源操作系統(tǒng)

2009-03-09 15:12:39

XenServer安裝

2024-10-16 09:34:50

2023-10-18 15:19:56

2010-03-03 17:44:07

Python多線程

2009-03-12 10:52:43

Java線程多線程

2011-07-28 14:19:12

iPhone 網(wǎng)絡(luò)編程 聊天程序

2023-04-02 17:53:10

多線程編程自測

2010-11-22 10:31:17

Sencha touc

2011-05-30 15:12:10

App Invento 初體驗

2023-06-05 07:56:10

線程分配處理器

2023-06-06 08:17:52

多線程編程Thread類

2023-06-07 13:49:00

多線程編程C#
點贊
收藏

51CTO技術(shù)棧公眾號