【Python】python多进程,函数内print的内容没有打印出来
2017-12-22 16:41
471 查看
问题: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:
解决: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
Multiprocessing example with a Pool:
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 multiprocessing def worker(num): print "worker ", num return jobs = [] 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: 0 Worker: 1 Worker: 2 Worker: 3 Worker: 4原因:spyder使用的stdout和windows不支持forking,所以无法打印子进程内容。The comments revealed that OP uses Windows as well as Spyder. Since Spyder redirects
stdoutand Windows does not support forking, a new child process won't print into the Spyder console. This is simply due to the fact that
stdoutof 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
multiprocessing.Manager().Queue()since forking is not possible) or more simply by relying on the multiprocessing Pool's
mapfunctionality, see example below.
Multiprocessing example with a Pool:
import multiprocessing def worker(num): """Returns the string of interest""" return "worker %d" % num def 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 0 worker 1 worker 2 worker 3 worker 4 worker 5 worker 6 worker 7 worker 8 worker 9EDIT: If you are to impatient to wait for the
mapfunction to finish, you can immediately print your results by using
imap_unorderedand 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
相关文章推荐
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- python help("modules") 打印help内容到文件的实现代码从pydoc中扒出来
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- 用java 和 python将希尔排序Shell's Sort 的每一步打印出来(每次的增量没有固定规则)
- 解决python3.6下scrapy中xpath.extract()匹配出来的内容转成json与.csv文件没有编码(unicode)的问题
- Python学习20:利用函数来打印文件内容
- [第一章]在Python中如何让两个print()函数的输出打印在一行内?
- PHP打印输出数组内容及结构函数print_r与var_dump
- Python打印print函数深入解析
- PHP打印输出数组内容及结构函数print_r与var_dump
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- 用window.print()函数打印网页指定区域的内容
- Python3.2中Print函数用法实例详解
- python函数参数是值传递还是引用传递(以及变量间复制后是否保持一致):取决于对象内容可变不可变
- python递归打印某个目录的内容