如何从multiprocessing.Process调用的函数返回数据? (Python3)

时间:2020-01-02 08:48:46

标签: python python-3.x process multiprocessing

如何从multiprocessing.Process调用的函数中返回数据

这里我在名为 thread_killer.py

的文件中有一个代码
from multiprocessing import Process
import time


def do_actions():
    i = 1
    # doing some long operation, if this operation will be done at less than 4 sec then it will return i to main() function
    while True:

        print(i)
        if i>2:
            break
        i += 1
        time.sleep(1)
    return "111" # this is my return 


def main(time_out):
    action_process = Process(target=do_actions)
    
    action_process.start() # Start the process which will be killed after "time_out" seconds.
    
    action_process.join(timeout=time_out)

    action_process.terminate()
    print("Timed is out.")

    return # Here I need to return "111" from do_actions function


if __name__ == '__main__':
    main(time_out)

这是我正在调用thread_killer的文件:

import thread_killer as tkl

if __name__ ==  '__main__':
    xxx = tkl.main(4)  
# (4) is amount of seconds after which thread called by multiprocessing.Process will be killed 
    print(xxx, "This is my return from do_actions() function passed to the main() function if the do_actions() function was fast and finished its job faster than if was terminated")

因此,当我运行xxx = tkl.main(4)时,我需要从do_actions()函数返回“ 111” 如何进行这项工作?

0 个答案:

没有答案