简体   繁体   English

不调用 Python 3 的多处理函数

[英]Multiprocessing function not calling Python 3

This is my code:这是我的代码:

from multiprocessing import Process, Manager, Value
from ctypes import c_char_p

def greet(string):
    print('bob')
    string.value = '1'
    for i in range(100):
        string.value = str(i)

if __name__ == '__main__':
    manager = Manager()
    string = manager.Value(c_char_p, "Hello")
    process = Process(target=greet, args=(string,))
    process.start()
    for j in range(100):
        print(string.value)
    process.join()
    input()

Now I expect the code to print something like:现在我希望代码打印如下内容:

Hello
1
1
1
4
4
5
5
6
7

Because, of course, I understand that both the loops will probably be running and different speeds.因为,当然,我知道两个循环都可能会运行并且速度不同。 But all the code prints is a Hello , a hundred times, it doesn't even print bob until the code ends and I call process.join() .但是所有打印的代码都是一个Hello ,一百次,它甚至在代码结束并且我调用process.join()之前都不会打印bob It is like the greet doesn't run until I call process.join() .就像在我调用process.join()之前, greet不会运行。 And I have read Python multiprocessing not calling function , and I am running the code from command line.我已经阅读了Python multiprocessing not called function ,并且我正在从命令行运行代码。 It still doesn't work它仍然不起作用

First of all, I'd like it if anyone could tell me why the function is being called only in the end, and how to fix it.首先,如果有人能告诉我为什么只在最后调用该函数,以及如何修复它,我会很高兴。 If that can be fixed, will string still be readable by both the parent and the child process?如果可以修复,父进程和子进程是否仍然可以读取string

Thanks in advance!提前致谢!

Your code is fine.你的代码没问题。 I think this really is just a timing problem on your specific machine.我认为这真的只是您特定机器上的计时问题。 When I ran your code, I could see ca.当我运行你的代码时,我可以看到 ca。 70x "hello" and then numbers. 70x“你好”,然后是数字。 So I started inserting sleep() s and it's really interesting to see what happens:所以我开始插入sleep() ,看看会发生什么真的很有趣:

from multiprocessing import Process, Manager, Value
from ctypes import c_char_p                                                 
import time                                                                 

def greet(string):                                                          
    print('bob')                                                            
    string.value = '1'                                                      
    for i in range(100):                                                    
        string.value = str(i)                                               
        time.sleep(0.1)

if __name__ == '__main__':                                                  
    manager = Manager()                                                     
    string = manager.Value(c_char_p, "Hello")                               
    process = Process(target=greet, args=(string,))                         
    process.start()                                                         
    time.sleep(0.5)
    for j in range(100):                                                    
        print(string.value)                                                 
        time.sleep(0.2)
    process.join()                                                          
    input()                                                                 

On my machine, I now get:在我的机器上,我现在得到:

bob
4
6
8
10
...

No "hello" because the sleep(0.5) in __main__ is virtually guaranteed to be slower than the newly forked "greet" process, so when finally the first print(string.value) is executed, the "hello" has long been overwritten.没有“hello”,因为__main__sleep(0.5)几乎可以保证比新分叉的“greet”进程慢,所以当最后执行第一个print(string.value) ,“hello”早已被覆盖。

Your code's fine, but your assumptions as to which part of the program takes how long are wrong (at least for your machine).您的代码很好,但是您对程序的哪一部分需要多长时间的假设是错误的(至少对于您的机器而言)。 Play with the sleeps a bit.玩一下睡眠。 It's very instructive.这是非常有指导意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM