Python多线程处理实例详解【单进程/多进程】

 更新时间:2019年01月30日 12:01:46   作者:徐为波  
这篇文章主要介绍了Python多线程处理,结合实例形式总结分析了Python单进程、多进程、多线程等相关操作技巧与注意事项,需要的朋友可以参考下

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下:

python — 多线程处理

1、一个进程执行完后,继续下一个进程

root@72132server:~# cd /root/python/multiprocess/
root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#

运行情况:

1)进程查看

root@72132server:~# cd /root/python/multiprocess/
root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24064 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24066 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24069 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24071 23930 0 20:45 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24073 23930 0 20:46 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess# ps -ef | grep multi
root   24075 23930 0 20:46 pts/3  00:00:00 grep multi
root@72132server:~/python/multiprocess#

2)脚本运行

root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# python multprocess.py
hello world!!! 0
hello world!!! 1
hello world!!! 2
hello world!!! 3
hello world!!! 4
hello world!!! 5
hello world!!! 6
hello world!!! 7
hello world!!! 8
hello world!!! 9
hello world!!! 10
hello world!!! 11

2、100个进行同时运行

root@72132server:~/python/multiprocess# ls
multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#
root@72132server:~/python/multiprocess# vi multprocess.py
root@72132server:~/python/multiprocess# cat multprocess.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
from multiprocessing import Process,Lock#启用多进程,与进程锁
import time,os
def sayhi(i):
  print 'hello world!!!', i
  time.sleep(10)
#lock = Lock()
for n in range(100):#执行n=100次
  p = Process(target=sayhi,args=(n,))#调用函数def,若def函数里面有参数,就是使用args带值赋值,若函数没有参数的话就args()为空。
  p.start()
  #p.join()#一个进程结束才会继续下一个进程。如果注释这句意思是一百个进程同时发起
root@72132server:~/python/multiprocess#

运行情况

1)进程查看

2)脚本运行(1秒跑完)

root@72132server:~/python/multiprocess# python multprocess.py
hello world!!! 0
hello world!!! 2
hello world!!! 3
hello world!!! 5
hello world!!! 7
hello world!!! 8
hello world!!! 6
hello world!!! 9
hello world!!! 10
hello world!!! 11
hello world!!! 14
hello world!!! 4
hello world!!! 15
hello world!!! 16
hello world!!! 1
hello world!!! 13
hello world!!! 18
hello world!!! 20
hello world!!! 19
hello world!!! 21
hello world!!! 12
hello world!!! 17
hello world!!! 23
hello world!!! 24
hello world!!! 26
hello world!!! 27
hello world!!! 22
hello world!!! 29
hello world!!! 31
hello world!!! 32
hello world!!! 33
hello world!!! 34
hello world!!! 28
hello world!!! 25
hello world!!! 30
hello world!!! 35
hello world!!! 36
hello world!!! 39
hello world!!! 41
hello world!!! 37
hello world!!! 40
hello world!!! 42
hello world!!! 43
hello world!!! 46
hello world!!! 47
hello world!!! 48
hello world!!! 38
hello world!!! 44
hello world!!! 45
hello world!!! 50
hello world!!! 51
hello world!!! 53
hello world!!! 54
hello world!!! 55
hello world!!! 57
hello world!!! 49
hello world!!! 58
hello world!!! 59
hello world!!! 60
hello world!!! 61
hello world!!! 62
hello world!!! 63
hello world!!! 64
hello world!!! 65
hello world!!! 66
hello world!!! 67
hello world!!! 68
hello world!!! 69
hello world!!! 56
hello world!!! 70
hello world!!! 52
hello world!!! 71
hello world!!! 72
hello world!!! 73
hello world!!! 76
hello world!!! 74
hello world!!! 78
hello world!!! 79
hello world!!! 80
hello world!!! 82
hello world!!! 77
hello world!!! 83
hello world!!! 84
hello world!!! 85
hello world!!! 86
hello world!!! 87
hello world!!! 81
hello world!!! 91
hello world!!! 75
hello world!!! 89
hello world!!! 92
hello world!!! 88
hello world!!! 90
hello world!!! 93
hello world!!! 95
hello world!!! 94
hello world!!! 96
hello world!!! 98
hello world!!! 9

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》、《Python+MySQL数据库程序设计入门教程》及《Python常见数据库操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • wxpython 学习笔记 第一天

    wxpython 学习笔记 第一天

    wxPython是Python编程语言的一个GUI工具箱。他使得Python程序员能够轻松的创建具有健壮、功能强大的图形用户界面的程序。
    2009-02-02
  • python爬虫使用scrapy注意事项

    python爬虫使用scrapy注意事项

    在本篇文章里小编给大家整理的是一篇关于python爬虫使用scrapy注意事项的相关文章,对此有兴趣的朋友们可以学习下。
    2020-11-11
  • python批量处理txt文件的实例代码

    python批量处理txt文件的实例代码

    这篇文章主要介绍了python批量处理txt文件的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • Django之importlib模块的具体使用

    Django之importlib模块的具体使用

    本文主要介绍了Django之importlib模块的具体使用,importlib模块是Python中用于动态加载和导入模块的内置模块,有需要了解jquery event事件用法的朋友可参考
    2023-11-11
  • python代码实现图书管理系统

    python代码实现图书管理系统

    这篇文章主要为大家详细介绍了python代码实现图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • python文件处理笔记之文本文件

    python文件处理笔记之文本文件

    相信大家在测试任务过程中都或多或少遇到自己处理文本文件的情况,这篇文章主要给大家介绍了关于python文件处理笔记之文本文件的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • 浅谈function(函数)中的动态参数

    浅谈function(函数)中的动态参数

    下面小编就为大家带来一篇浅谈function(函数)中的动态参数。小编觉得听不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题

    新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题

    这篇文章主要介绍了解决新版Pycharm中Matplotlib不会弹出独立的显示窗口的问题,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Python文件相关操作和方法汇总大全

    Python文件相关操作和方法汇总大全

    这篇文章主要介绍了Python文件相关操作和方法汇总大全,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Python导入模块的3种方式小结

    Python导入模块的3种方式小结

    本文主要介绍了Python导入模块的3种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03

最新评论