会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 Python多线程编程初体验!

Python多线程编程初体验

时间:2025-11-04 23:21:36 来源:益强数据堂 作者:应用开发 阅读:189次

 前言

这将是多线一个系列,一个关于进程、程编程初线程和 协程的体验系列。

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

查看线程ID

创建文件 0809.py

import time import threading def loop():     while True:         print(thread id is {}.format(threading.get_native_id()))         time.sleep(3) if __name__ == __main__:     loop() 

在第一个终端窗口中执行

$ python 0809.py  thread id is 3344 thread id is 3344 thread id is 3344 ······ 

在第二个终端窗口中执行

ps -ef | grep python 0809.py vagrant   3344  3117  0 16:26 pts/1    00:00:00 python 0809.py 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。

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

# 0809.py  import time import threading count = 0 def consumer():     global count     while True:         if count <= 0:             continue         count = count - 1         print(fcount is {count},程编程初 consumer thread id is {threading.get_native_id()})         time.sleep(2) def producer():     global count     while True:         count = count + 1         print(fcount is {count}, producer thread id is {threading.get_native_id()})         time.sleep(1) if __name__ == __main__:     tp = threading.Thread(target=producer)     tc = threading.Thread(target=consumer)     tp.start()     tc.start() 

执行命令 python 0809.py

$ python 0809.py  count is 1, producer thread id is 3785 count is 0, consumer thread id is 3786 count is 1, producer thread id is 3785 count is 0, consumer thread id is 3786 count is 1, producer thread id is 3785 count is 2, producer thread id is 3785 count is 1, consumer thread id is 3786 count is 2, producer thread id is 3785 

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

我们再来查看一下相关的网站模板程编程初进程和线程

$ ps -ef | grep python 0809.py vagrant   3784  3117  0 17:24 pts/1    00:00:00 python 0809.py vagrant   3789  3451  0 17:24 pts/0    00:00:00 grep --color=auto python 0809.py $ ps -T -p 3784   PID  SPID TTY          TIME CMD  3784  3784 pts/1    00:00:00 python  3784  3785 pts/1    00:00:00 python  3784  3786 pts/1    00:00:00 python 

可以看出该进程中有三个线程,分别是体验主线程 3784 和两个子线程 3785(producer)、3786(consumer)

今天我们就先讲到这里,重点掌握:

1、如何在python代码中和shell终端中查看线程id 进程ID 以及进程中包含的线程。

2、理解生产/消费者模型,因为这个模型会在接下来的学习中被多次提到

(责任编辑:IT科技类资讯)

推荐内容
  • 电脑报read错误的解决方法(遇到电脑报read错误如何应对,尝试这些方法解决问题)
  • 如何进行BIOS备份教程(轻松学会BIOS备份,保障您的计算机安全)
  • 解决电脑打开硬盘显示属性错误的方法(如何处理硬盘显示属性错误和恢复数据)
  • 解决Lenovo电脑错误的有效方法(针对不同类型Lenovo电脑错误的解决方案)
  • 电脑关机提示端口错误的原因及解决方法(探究电脑关机时显示端口错误的常见原因和解决办法)
  • 解决电脑打印报表显示错误的方法(排查和修复电脑打印报表显示错误的实用技巧)