python 多进程
2017-10-18 09:31
330 查看
python多线程 不适合cpu密集操作型的任务 适合io操作密集型的任务。
起多个进程
运行结果
在进程里面启动线程
运行结果
multiprocessing 多进程
import multiprocessing import time def run(name): print('hello',name) if __name__ == '__main__': p = multiprocessing.Process(target=run,args=('jim',)) p.start() #hello jim
起多个进程
import multiprocessing import time def run(name): time.sleep(1) print('hello',name) if __name__ == '__main__': for i in range(10): p = multiprocessing.Process(target=run,args=('jim %s' %i,)) p.start()
运行结果
hello jim 0 hello jim 3 hello jim 2 hello jim 1 hello jim 6 hello jim 7 hello jim 4 hello jim 8 hello jim 5 hello jim 9
在进程里面启动线程
import multiprocessing import time,threading def run1(): print("当前线程[%s]"% threading.get_ident()) def run(name): print('当前进程 ',name) t = threading.Thread(target=run1) t.start() if __name__ == '__main__': for i in range(10): p = multiprocessing.Process(target=run,args=(' %s' %i,)) p.start()
运行结果
当前进程 2 当前线程[8168] 当前进程 1 当前线程[2260] 当前进程 0 当前线程[2012] 当前进程 3 当前线程[716] 当前进程 5 当前线程[6888] 当前进程 4 当前线程[1504] 当前进程 6 当前线程[8096] 当前进程 9 当前线程[4576] 当前进程 7 当前线程[5368] 当前进程 8 当前线程[6284]
相关文章推荐
- Python 多进程 multiprocessing.Pool类详解
- Python多进程同步Lock、Semaphore、Event实例
- Python学习-并发编程之多进程
- python多线程、异步、多进程+异步爬虫
- python 多进程开发与多线程开发
- python 多进程 multiprocessing
- python实现多进程
- python Multiprocessing 多进程应用
- Python 多进程 多线程数据共享
- Python模块整理(五):多进程multiprocessing
- python 多线程多进程
- python多进程中的内存复制(实例讲解)
- python多进程实现进程间通信实例
- Python多进程multiprocessing.Pool
- python服务端多进程压测工具
- Python多进程Pool相关函数<转>
- Python多线程与多进程
- 在Python中使用多进程
- Python多进程
- python3 语法之多进程