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

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

直接上代码:

  1. from tqdm import tqdm
  2. from multiprocessing import Pool
  3. import functools
  4. from pymongo import MongoClient
  5. mdb = MongoClient('120.xx.26.xx:20002', username='xx', password='xxxxx')
  6.  
  7. # 三种main的写法只写一种即可
  8.  
  9. def create_data(image):
  10.  
  11. # TODO 具体处理逻辑
  12. print(image)
  13. return str(image)
  14.  
  15. def main_deal():
  16.  
  17. num_processor = 20
  18. p = Pool(num_processor)
  19. images = mdb['db_name']['image'].find(no_cursor_timeout=True).batch_size(200)
  20.  
  21. fw = open('result.txt', 'w+')
  22. for result in tqdm(p.imap(create_data, images), total=images.count()):
  23. fw.write(result + '\n')
  24. fw.close()
  25.  
  26. for _ in tqdm(p.imap_unordered(create_data, images)):
  27. pass
  28. p.close()
  29. p.join()
  30.  
  31. def main_deal():
  32.  
  33. num_processor = 20
  34. p = Pool(num_processor)
  35.  
  36. images = mdb['goodlook']['image_generated_data'].find(no_cursor_timeout=True).batch_size(200)
  37. fw = open('result.txt', 'w+')
  38. for result in tqdm(p.imap_unordered(create_data, images)):
  39. fw.write(result + '\n')
  40. fw.close()
  41.  
  42. p.close()
  43. p.join()
  44.  
  45. def main_deal():
  46. num_processor = 20
  47. p = Pool(num_processor)
  48.  
  49. images = mdb['goodlook']['image_generated_data'].find(no_cursor_timeout=True).batch_size(200)
  50. fw = open('result.txt', 'w+')
  51.  
  52. pt = functools.partial(create_data)
  53. for result in tqdm(p.imap_unordered(pt, images)):
  54. fw.write(result + '\n')
  55. fw.close()
  56.  
  57. p.close()
  58. p.join()
  59.  
  60. if __name__ == '__main__':
  61. 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. java 字符串压缩长度并解压

    package com.wy.data.emaildata.util; import org.apache.commons.io.FileUtils; import java.io.ByteArray ...

  2. 为VLC增加在线字幕插件VLSub

    VLC的在在线字幕插件VLSub,官网:https://github.com/exebetche/vlsub. 原理是通过搜索全球最大的字幕网站https://www.opensubtitles.or ...

  3. Android在其他线程中更新UI

    public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...

  4. module has no attribute 'seq2seq'

    tensorflow 中tf.nn.seq2seq.sequence_loss_by_example to tf.contrib.legacy_seq2seq.sequence_loss_by_exa ...

  5. ubuntu安装 uwsgi

    http://www.jianshu.com/p/e6ff4a28ab5a/ sudo apt-get install python-dev #不安装这个,下面的安装可能会失败 sudo pip in ...

  6. 如何给redis设置密码

    如何给redis设置密码 学习了:https://blog.csdn.net/qq_35357001/article/details/56835919

  7. Codeforces Round #313 (Div. 2) ABC

    A http://codeforces.com/contest/560/problem/A 推断给出的数能否组成全部自然数. 水题 int a[1010]; bool b[1000010]; int ...

  8. Vue2.0 视频教程

    好像是一套vue 开发webapp 课程.来自网络. url:https://pan.baidu.com/s/1jIele9w password:b404 文章来源:刘俊涛的博客 地址:http:// ...

  9. 每天进步一点点——Linux中的线程局部存储(二)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/26876231     在Linux中另一种更为高效的线程局部存储方法,就是使用keyword ...

  10. vs 编译错误 The name 'InitializeComponent' does not exist in the current context in WPF application

    1:文件命名空间的问题 xaml文件和model.cs文件的命名空间 2:csproj 那么它究竟是给谁用的呢?那是给开发工具用的,例如我们在熟悉不过的Visual Studio,以及大家可以没有接触 ...