您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

【Python有坑系列】python多进程,函数内print的内容没有打印出来

5b51 2022/1/14 8:25:21 python 字数 18878 阅读 923 来源 www.jb51.cc/python

问题:python多进程,子函数内容没有打印出来。 SimplePythonMultiprocessingfunctiondoesn\'toutputresults

概述

问题: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:

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#303336;">[<<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#2b91af;">Process<span style="color:#303336;">-<span style="color:#7d2727;">12<span style="color:#303336;">,<span style="color:#303336;"> stopped<span style="color:#303336;">[<span style="color:#7d2727;">1@H_40421@,<span style="color:#303336;">
<span style="color:#303336;"><<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#2b91af;">Process<span style="color:#303336;">-<span style="color:#7d2727;">13<span style="color:#303336;">,<span style="color:#303336;">
<span style="color:#303336;"><<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#2b91af;">Process<span style="color:#303336;">-<span style="color:#7d2727;">14<span style="color:#303336;">,<span style="color:#303336;">
<span style="color:#303336;"><<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#2b91af;">Process<span style="color:#303336;">-<span style="color:#7d2727;">15<span style="color:#303336;">,<span style="color:#303336;">
<span style="color:#303336;"><<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#2b91af;">Process<span style="color:#303336;">-<span style="color:#7d2727;">16<span style="color:#303336;">,<span style="color:#303336;"> stopped<span style="color:#303336;">[<span style="color:#7d2727;">1@H
404_21@]

Here's the code:

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#101094;">import<span style="color:#303336;"> multiprocessing

<span style="color:#101094;">def<span style="color:#303336;"> worker<span style="color:#303336;">(<span style="color:#303336;">num<span style="color:#303336;">):<span style="color:#303336;">
<span style="color:#101094;">print<span style="color:#303336;"> <span style="color:#7d2727;">"worker "<span style="color:#303336;">,<span style="color:#303336;"> num
<span style="color:#101094;">return<span style="color:#303336;">

jobs <span style="color:#303336;">=<span style="color:#303336;"> <span style="color:#303336;">[]<span style="color:#303336;">
<span style="color:#101094;">for<span style="color:#303336;"> i <span style="color:#101094;">in<span style="color:#303336;"> range<span style="color:#303336;">(<span style="color:#7d2727;">5<span style="color:#303336;">):<span style="color:#303336;">
p <span style="color:#303336;">=<span style="color:#303336;"> multiprocessing<span style="color:#303336;">.<span style="color:#2b91af;">Process<span style="color:#303336;">(<span style="color:#303336;">target <span style="color:#303336;">=<span style="color:#303336;"> worker<span style="color:#303336;">,<span style="color:#303336;"> args <span style="color:#303336;">=<span style="color:#303336;"> <span style="color:#303336;">(<span style="color:#303336;">i<span style="color:#303336;">,))<span style="color:#303336;">
jobs<span style="color:#303336;">.<span style="color:#303336;">append<span style="color:#303336;">(<span style="color:#303336;">p<span style="color:#303336;">)<span style="color:#303336;">
p<span style="color:#303336;">.<span style="color:#303336;">start<span style="color:#303336;">()

Here's the result I'm expecting but it's not outputting anything:

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#2b91af;">Worker<span style="color:#303336;">:<span style="color:#303336;"> <span style="color:#7d2727;">0<span style="color:#303336;">
<span style="color:#2b91af;">Worker<span style="color:#303336;">:<span style="color:#303336;"> <span style="color:#7d2727;">1<span style="color:#303336;">
<span style="color:#2b91af;">Worker<span style="color:#303336;">:<span style="color:#303336;"> <span style="color:#7d2727;">2<span style="color:#303336;">
<span style="color:#2b91af;">Worker<span style="color:#303336;">:<span style="color:#303336;"> <span style="color:#7d2727;">3<span style="color:#303336;">
<span style="color:#2b91af;">Worker<span style="color:#303336;">:<span style="color:#303336;"> <span style="color:#7d2727;">4

原因:spyder使用的stdout和windows不支持forking,所以无法打印子进程内容

stdoutstdoutsys.__stdout__

There are two alternatives:

<ol style="margin-left:30px;">

Using the  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  (or multiprocessing.Manager().Queue() since forking is not possible) or more simply by relying on the  map functionality,see example below.

Multiprocessing example with a Pool:

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#101094;">import<span style="color:#303336;"> multiprocessing

<span style="color:#101094;">def<span style="color:#303336;"> worker<span style="color:#303336;">(<span style="color:#303336;">num<span style="color:#303336;">):<span style="color:#303336;">
<span style="color:#7d2727;">"""Returns the string of interest"""<span style="color:#303336;">
<span style="color:#101094;">return<span style="color:#303336;"> <span style="color:#7d2727;">"worker %d"<span style="color:#303336;"> <span style="color:#303336;">%<span style="color:#303336;"> num

<span style="color:#101094;">def<span style="color:#303336;"> main<span style="color:#303336;">():<span style="color:#303336;">
pool <span style="color:#303336;">=<span style="color:#303336;"> multiprocessing<span style="color:#303336;">.<span style="color:#2b91af;">Pool<span style="color:#303336;">(<span style="color:#7d2727;">4<span style="color:#303336;">)<span style="color:#303336;">
results <span style="color:#303336;">=<span style="color:#303336;"> pool<span style="color:#303336;">.<span style="color:#303336;">map<span style="color:#303336;">(<span style="color:#303336;">worker<span style="color:#303336;">,<span style="color:#303336;"> range<span style="color:#303336;">(<span style="color:#7d2727;">10<span style="color:#303336;">))<span style="color:#303336;">

pool</span><span style="color:#303336;"&gt;.</span><span style="color:#303336;"&gt;close</span><span style="color:#303336;"&gt;()</span><span style="color:#303336;"&gt;
pool</span><span style="color:#303336;"&gt;.</span><span style="color:#303336;"&gt;join</span><span style="color:#303336;"&gt;()</span><span style="color:#303336;"&gt;

</span><span style="color:#101094;"&gt;for</span><span style="color:#303336;"&gt; result </span><span style="color:#101094;"&gt;in</span><span style="color:#303336;"&gt; results</span><span style="color:#303336;"&gt;:</span><span style="color:#303336;"&gt;
    </span><span style="color:#858c93;"&gt;# prints the result string in the main process</span><span style="color:#303336;"&gt;
    </span><span style="color:#101094;"&gt;print</span><span style="color:#303336;"&gt;(</span><span style="color:#303336;"&gt;result</span><span style="color:#303336;"&gt;)</span><span style="color:#303336;"&gt;

<span style="color:#101094;">if<span style="color:#303336;"> name <span style="color:#303336;">==<span style="color:#303336;"> <span style="color:#7d2727;">'main'<span style="color:#303336;">:<span style="color:#303336;">
<span style="color:#858c93;"># Better protect your main function when you use multiprocessing<span style="color:#303336;">
main<span style="color:#303336;">()

which prints (in the main process)

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#303336;">worker <span style="color:#7d2727;">0<span style="color:#303336;">
worker <span style="color:#7d2727;">1<span style="color:#303336;">
worker <span style="color:#7d2727;">2<span style="color:#303336;">
worker <span style="color:#7d2727;">3<span style="color:#303336;">
worker <span style="color:#7d2727;">4<span style="color:#303336;">
worker <span style="color:#7d2727;">5<span style="color:#303336;">
worker <span style="color:#7d2727;">6<span style="color:#303336;">
worker <span style="color:#7d2727;">7<span style="color:#303336;">
worker <span style="color:#7d2727;">8<span style="color:#303336;">
worker <span style="color:#7d2727;">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:

<pre class="lang-py prettyprint prettyprinted">
<span style="color:#101094;">def<span style="color:#303336;"> main<span style="color:#303336;">():<span style="color:#303336;">
pool <span style="color:#303336;">=<span style="color:#303336;"> multiprocessing<span style="color:#303336;">.<span style="color:#2b91af;">Pool<span style="color:#303336;">(<span style="color:#7d2727;">4<span style="color:#303336;">)<span style="color:#303336;">
results <span style="color:#303336;">=<span style="color:#303336;"> pool<span style="color:#303336;">.<span style="color:#303336;">imap_unordered<span style="color:#303336;">(<span style="color:#303336;">worker<span style="color:#303336;">,<span style="color:#303336;"> range<span style="color:#303336;">(<span style="color:#7d2727;">10<span style="color:#303336;">))<span style="color:#303336;">

</span><span style="color:#101094;"&gt;for</span><span style="color:#303336;"&gt; result </span><span style="color:#101094;"&gt;in</span><span style="color:#303336;"&gt; results</span><span style="color:#303336;"&gt;:</span><span style="color:#303336;"&gt;
    </span><span style="color:#858c93;"&gt;# prints the result string in the main process as soon as say are ready</span><span style="color:#303336;"&gt;
    </span><span style="color:#858c93;"&gt;# but results are <a href="https://www.jb51.cc/tag/Now/" target="_blank" class="keywords">Now</a> no longer in order!</span><span style="color:#303336;"&gt;
    </span><span style="color:#101094;"&gt;print</span><span style="color:#303336;"&gt;(</span><span style="color:#303336;"&gt;result</span><span style="color:#303336;"&gt;)</span><span style="color:#303336;"&gt;

</span><span style="color:#858c93;"&gt;# The pool should join after printing all results</span><span style="color:#303336;"&gt;
pool</span><span style="color:#303336;"&gt;.</span><span style="color:#303336;"&gt;close</span><span style="color:#303336;"&gt;()</span><span style="color:#303336;"&gt;
pool</span><span style="color:#303336;"&gt;.</span><span style="color:#303336;"&gt;join</span><span style="color:#303336;"&gt;()</span></code></pre>

来源:https://stackoverflow.com/questions/29629103/simple-python-multiprocessing-function-doesnt-output-results/29632397#29632397

总结

以上是编程之家为你收集整理的【Python有坑系列】python多进程,函数内print的内容没有打印出来全部内容,希望文章能够帮你解决【Python有坑系列】python多进程,函数内print的内容没有打印出来所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶