python 之 多线程、多进程代码
2017-11-28 16:09
232 查看
thread-多线程
multiprocessing-多进程
multiprocessing-多进程
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 import os 4 import threading 5 import multiprocessing 6 count_thread = 0 7 count_process = 0 8 9 # worker function 10 def worker1(sign, lock): 11 global count_thread 12 lock.acquire() 13 count_thread += 1 14 print(sign, os.getpid()) 15 lock.release() 16 17 def worker2(sign, lock): 18 global count_process 19 lock.acquire() 20 count_process += 1 21 print(sign, os.getpid()) 22 lock.release() 23 # Main 24 print('Main:',os.getpid()) 25 26 # Multi-thread 27 record = [] 28 lock = threading.Lock() 29 for i in range(5): 30 thread = threading.Thread(target=worker1,args=('thread',lock)) 31 thread.start() 32 record.append(thread) 33 34 for thread in record: 35 thread.join() 36 37 # Multi-process 38 record = [] 39 lock = multiprocessing.Lock() 40 for i in range(5): 41 process = multiprocessing.Process(target=worker2,args=('process',lock)) 42 process.start() 43 record.append(process) 44 45 for process in record: 46 process.join() 47 48 49 print count_thread 50 print count_process
相关文章推荐
- Python多线程、异步+多进程爬虫实现代码
- python的多线程、多进程代码示例
- Python多线程、异步+多进程爬虫实现代码
- Python多线程、异步+多进程爬虫实现代码
- Python守护进程(daemon)代码实例
- python爬虫实战--selenium验证码保存+多线程多标签+自动点击+完整代码
- python 简单的多线程链接实现代码
- Python多线程和多进程
- python --- 多线程处理(单进程/多进程)
- Python串行运算、并行运算、多线程、多进程对比实验
- python多线程和多进程
- python实现多线程采集的2个代码例子
- Python 30 行代码实现小型多线程任务队列
- python实现多线程暴力破解登陆路由器功能代码分享
- 线程和进程的区别及Python代码实例
- 【代码】廖雪峰Python教程,简单分布式进程
- Python 多进程和多线程
- Python 爬虫多线程详解及实例代码
- python的多线程与多进程介绍及多进程详解(调用multiprocessing组件)
- python多线程与多进程的概念与区别