python 多进程和多线程
来源:互联网 发布:安卓java计算器源码 编辑:程序博客网 时间:2024/05/18 20:49
多进程和多线程
#!/usr/bin/python# -*- coding:utf-8 -*-import osimport threadingimport multiprocessingimport time#Process entrydef process_worker(sign,lock,function): global count_process #lock.acquire() count_process += 1 print(sign, os.getpid()) print apply(function) #lock.release()#Thread entrydef thread_worker(sign, lock,function): global count_thread lock.acquire() count_thread += 1 print(sign, os.getpid()) print apply(function) lock.release() #real work functions def Lee(): return 'I am Lee'def Marlon(): return 'I am Marlon'def Allen(): return 'I am Allen' count_thread = 0count_process = 0if __name__=='__main__': # Multi-process begin multiprocessing.freeze_support() #window python 中需要,否则会出现随意打开很多进程的情况 print('Main:',os.getpid()) record = [] lock = multiprocessing.Lock() func_list = [Lee,Marlon,Allen] for function in func_list: process = multiprocessing.Process(target=process_worker,args=('Process',lock,function)) process.start() record.append(process) for process in record: process.join() print "count_process value is", count_process #该值没有改变,多进程不共享内存 # Multi-thread begin record = [] lock = threading.Lock() for function in func_list: thread = threading.Thread(target=thread_worker,args=('thread',lock,function)) thread.start() record.append(thread) for thread in record: thread.join() print "count_thread value is", count_thread #该值改变,所以多线程共享内存
0 0
- python 多进程和多线程
- python 多线程和多进程
- python多进程和多线程
- Python 多进程和多线程
- Python多线程和多进程
- Python多进程和多线程
- Python中的GIL、多进程和多线程
- python的多线程和多进程
- Python :多线程和多进程的选择
- python中的多线程和多进程
- Python多线程和多进程谁更快?
- Python多线程和多进程谁更快?
- 搞定python多线程和多进程
- python的多进程和多线程
- Python多线程和多进程编程
- Python 多线程和多进程对比
- Python中的多进程和多线程
- python 多线程多进程
- Oracle事务的总结
- java并发编程实践之安全发布和逸出
- ubuntu安装配置笔记
- HBase基础和伪分布式安装配置
- html图片翻转,图片剪裁
- python 多进程和多线程
- 实习的是那些事儿一
- 基于Qt移动应用的消息推送服务原理与应用
- keil多文件组织方法 - 道客巴巴
- jQuery替换textarea中的换行(转)
- 离职的正确原因
- [LeetCode]Binary Tree Level Order Traversal
- JSP分页(MySql+c3p0+dbutils)
- WebDriver中执行JavaScript