【Python】python多进程,函数内print的内容没有打印出来
来源:互联网 发布:阿尔卑斯牧场物语java 编辑:程序博客网 时间:2024/06/04 19:50
问题:python多进程,子函数内容没有打印出来。
Simple Python Multiprocessing function doesn't output results
I have this very simple function right here in which I'm trying to run and test on, however, it doesn't output anything and it doesn't have any errors either. I've checked the code multiple times but it doesn't have any errors.
I printed jobs and here's what I got:
[<Process(Process-12, stopped[1])>, <Process(Process-13, stopped[1])>,<Process(Process-14, stopped[1])>, <Process(Process-15, stopped[1])>,<Process(Process-16, stopped[1])>]
Here's the code:
import multiprocessingdef worker(num): print "worker ", num returnjobs = []for i in range(5): p = multiprocessing.Process(target = worker, args = (i,)) jobs.append(p) p.start()
Here's the result I'm expecting but it's not outputting anything:
Worker: 0Worker: 1Worker: 2Worker: 3Worker: 4
原因:spyder使用的stdout和windows不支持forking,所以无法打印子进程内容。The comments revealed that OP uses Windows as well as Spyder. Since Spyder redirects stdout
and Windows does not support forking, a new child process won't print into the Spyder console. This is simply due to the fact that stdout
of the new child process is Python's vanilla stdout, which can also be found in sys.__stdout__
.
解决:1.打印日志到文件。2.用返回值替代打印。
There are two alternatives:
Using the logging module. This would encompass creating and logging all messages to one or several files. Using a single log-file may lead to the problem that the output is slightly garbled since the processes would write concurrently to the file. Using a single file per process could solve this.
Not using
print
within the child processes, but simply returning the result to the main process. Either by using a queue (ormultiprocessing.Manager().Queue()
since forking is not possible) or more simply by relying on the multiprocessing Pool'smap
functionality, see example below.
Multiprocessing example with a Pool:
import multiprocessingdef worker(num): """Returns the string of interest""" return "worker %d" % numdef main(): pool = multiprocessing.Pool(4) results = pool.map(worker, range(10)) pool.close() pool.join() for result in results: # prints the result string in the main process print(result)if __name__ == '__main__': # Better protect your main function when you use multiprocessing main()
which prints (in the main process)
worker 0worker 1worker 2worker 3worker 4worker 5worker 6worker 7worker 8worker 9
EDIT: If you are to impatient to wait for the map
function to finish, you can immediately print your results by using imap_unordered
and slightly changing the order of the commands:
def main(): pool = multiprocessing.Pool(4) results = pool.imap_unordered(worker, range(10)) for result in results: # prints the result string in the main process as soon as say are ready # but results are now no longer in order! print(result) # The pool should join after printing all results pool.close() pool.join()
来源:https://stackoverflow.com/questions/29629103/simple-python-multiprocessing-function-doesnt-output-results/29632397#29632397 - 【Python】python多进程,函数内print的内容没有打印出来
- [第一章]在Python中如何让两个print()函数的输出打印在一行内?
- Python的print函数
- Python打印print函数深入解析
- Python的Print打印中文乱码解决方案
- Python的Print打印中文乱码解决方案
- python含有中文的list如何print出来
- python help("modules") 打印help内容到文件的实现代码从pydoc中扒出来
- Python print 不换行打印
- Python print 不换行打印
- Python print打印不换行
- Python print 不换行打印
- Python Print 打印不换行
- Python: print 不换行打印
- Python print函数用法
- Python Print函数用法
- Python Print函数用法
- python print函数
- Ocupload的一点改动
- c LINUX环境USB3.0 侦测试程式
- 阿里云服务器部署Javaweb项目(四)
- DevExpress Dashboard入门教程(一):绑定Oracle数据库
- Windows XP环境下IPSec VPN的配置
- 【Python】python多进程,函数内print的内容没有打印出来
- 基于Token的WEB后台认证机制
- 该如何解除PDF文档的密码呢
- Struts2笔记
- 前端学记之CSS-引入方式、语法、浏览器私有属性等
- javascript获取Document类型和Element类型属性
- Ubuntu 软件安装和管理
- jquery展示今年与去年的月份
- laravel学习笔记(二)