https://blog.csdn.net/qq_39694935/article/details/84552076

【Python】multiprocessing Pool 进程间通信共享

直接上代码:

from tqdm import tqdm
from multiprocessing import Pool
import functools
from pymongo import MongoClient
mdb = MongoClient('120.xx.26.xx:20002', username='xx', password='xxxxx') # 三种main的写法只写一种即可 def create_data(image): # TODO 具体处理逻辑
print(image)
return str(image) def main_deal(): num_processor = 20
p = Pool(num_processor)
images = mdb['db_name']['image'].find(no_cursor_timeout=True).batch_size(200) fw = open('result.txt', 'w+')
for result in tqdm(p.imap(create_data, images), total=images.count()):
fw.write(result + '\n')
fw.close() for _ in tqdm(p.imap_unordered(create_data, images)):
pass
p.close()
p.join() def main_deal(): num_processor = 20
p = Pool(num_processor) images = mdb['goodlook']['image_generated_data'].find(no_cursor_timeout=True).batch_size(200)
fw = open('result.txt', 'w+')
for result in tqdm(p.imap_unordered(create_data, images)):
fw.write(result + '\n')
fw.close() p.close()
p.join() def main_deal():
num_processor = 20
p = Pool(num_processor) images = mdb['goodlook']['image_generated_data'].find(no_cursor_timeout=True).batch_size(200)
fw = open('result.txt', 'w+') pt = functools.partial(create_data)
for result in tqdm(p.imap_unordered(pt, images)):
fw.write(result + '\n')
fw.close() p.close()
p.join() if __name__ == '__main__':
main_deal()

  

python多进程执行任务的更多相关文章

  1. Python多进程编程

    转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...

  2. Python多进程(1)——subprocess与Popen()

    Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...

  3. Python多进程使用

    [Python之旅]第六篇(六):Python多进程使用   香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要:   关于进程与线程的对比, ...

  4. Python多进程并发(multiprocessing)用法实例详解

    http://www.jb51.net/article/67116.htm 本文实例讲述了Python多进程并发(multiprocessing)用法.分享给大家供大家参考.具体分析如下: 由于Pyt ...

  5. python 多进程开发与多线程开发

    转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文:  博文1  博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...

  6. day-4 python多进程编程知识点汇总

    1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...

  7. 【转】Python多进程编程

    [转]Python多进程编程 序. multiprocessingpython中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Pytho ...

  8. Python 多进程multiprocessing

    一.python多线程其实在底层来说只是单线程,因此python多线程也称为假线程,之所以用多线程的意义是因为线程不停的切换这样比串行还是要快很多.python多线程中只要涉及到io或者sleep就会 ...

  9. Python多进程池 multiprocessing Pool

    1. 背景 由于需要写python程序, 定时.大量发送htttp请求,并对结果进行处理. 参考其他代码有进程池,记录一下. 2. 多进程 vs 多线程 c++程序中,单个模块通常是单进程,会启动几十 ...

随机推荐

  1. 微信小程序 压缩图片并上传

    转自https://segmentfault.com/q/1010000012507519 wxml写入 <view bindtap='uploadImg'>上传</view> ...

  2. android 根据图片名字获取图片id

    public int getResource(String imageName){ Context ctx=getBaseContext(); int resId = getResources().g ...

  3. iOS -- YYText富文本

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: [NSString strin ...

  4. cygwin搭建ssh服务器

    下载cygwin的setup.exe安装包

  5. java要在命令行执行eclipse的项目的方法

    在命令行运行eclipse的项目时须要把该项目生成一个能够运行的jar包,才干够在命令行下运行:分为两种情况,一种是项目中没有调用第三方的jar包,这样的比較简单.网上的资源也非常多,本文主要讲述第二 ...

  6. flex操作XML,强力总结帖

    初始化XML对象 XML对象可以代表一个XML元素.属性.注释.处理指令或文本元素.在ActionScript 3.0中我们可以直接将XML数据赋值给变量: var myXML:XML =    &l ...

  7. OpenCV for Python 学习笔记 一

    本人的学习笔记主要记录的是学习opencv-python-tutorials这本书中的笔记 今天晚上简单学习OpenCV for Python如何绘图,主要用了这几个函数(这几个函数可在:http:/ ...

  8. 运维基础-IO 管道

    什么是文件描述符FD或者文件句柄? 通过构建一个带有编号标记的通道(文件描述符)的进程结构来管理打开的文件.今晨连接到文件,从而达到这些文件所代表的的数据内容或者设备.通过使用通道0.1.2(称为标准 ...

  9. THE MARTIAN

    影片的最后一段自白 When I was up there, stranded by myself …… “did I think I was going to die?” Yes, absolute ...

  10. 关于CSS和CSS3的布局小知识(干货)

    最近在网站偶然看到的这个网站,进去看了下讲的CSS布局,感觉还不错,讲易懂且实用推荐给大家. http://zh.learnlayout.com/