Python多线程编程初体验

开发 后端
这将是一个系列,一个关于进程、线程和 协程的系列。主要用于:回顾和复习以往所学的知识 以及 希望这点经验能够帮助正在学习编程的你

 前言

这将是一个系列,一个关于进程、线程和 协程的系列。

主要用于:回顾和复习以往所学的知识 以及 希望这点经验能够帮助正在学习编程的你

[[417145]]

查看线程ID

创建文件 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() 

 

在第一个终端窗口中执行

 

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

 

在第二个终端窗口中执行

 

  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 

 

你会发现其进程ID也是 3344和线程ID一致。这是因为Linux中规定,当一个进程中只有一个线程的情况下,线程ID等于进程ID。或则说,进程的第一个线程(主线程)的ID等于进程ID。

经典的生产者/消费者模型(也有人称之为,发布/订阅模型)

 

  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() 

 

执行命令 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 

可以发现,两个线程并非严格交替执行,而是随机执行。

我们再来查看一下相关的进程和线程

 

  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、理解生产/消费者模型,因为这个模型会在接下来的学习中被多次提到

责任编辑:华轩 来源: 今日头条
相关推荐

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-10-18 15:19:56

2023-06-13 13:39:00

多线程异步编程

2010-03-03 17:44:07

Python多线程

2009-03-12 10:52:43

Java线程多线程

2009-03-09 15:12:39

XenServer安装

2009-08-01 09:06:35

UbuntuOneLinux开源操作系统

2011-07-28 14:19:12

iPhone 网络编程 聊天程序

2023-06-06 08:17:52

多线程编程Thread类

2023-06-05 07:56:10

线程分配处理器

2023-04-02 17:53:10

多线程编程自测

2023-06-07 13:49:00

多线程编程C#

2023-07-15 08:01:38

2010-11-22 10:31:17

Sencha touc

2011-05-30 15:12:10

App Invento 初体验
点赞
收藏

51CTO技术栈公众号