简体   繁体   English

如何在 python 的另一个函数中启动进程?

[英]How can i start process inside another function in python?

Suppose i have two functions假设我有两个功能

def funct1():
    ##does something
def funct2():
    ##does something

I want to use them in another function with multiprocessing like so:我想在另一个具有多处理功能的函数中使用它们,如下所示:

def my_funct
    ##does something
    if __name__ == '__main__':
         p1 = Process(target = funct1)
         p2 = Process(target = funct2)
         p1.start()
         p2.start()
         ##more code
         p1.terminate()
         p2.terminate()
     return something

Basically i want to start and end processes inside my function but its not working properly.基本上我想在我的函数中启动和结束进程,但它不能正常工作。 What would be the correct way to do this?这样做的正确方法是什么?

On platforms that use the spawn method to create new processes it becomes necessary to place the process-creation code that exists at the global scope within an if __name__ == '__main__': block to prevent the newly created child process from trying to re-execute recursively the process-creation code since all code at the global level is re-executed in order to initialize memory (eg function definitions and global variables) for the process.在使用spawn方法创建新进程的平台上,有必要将存在于全局范围内的进程创建代码放在if __name__ == '__main__':块中,以防止新创建的子进程尝试重新-递归地执行进程创建代码,因为全局级别的所有代码都被重新执行以初始化进程的内存(例如函数定义和全局变量)。 Putting such a test within a function or method, which by definition would not be at global scope, would not normally make too much sense.将这样的测试放在一个函数或方法中,根据定义,它不会在全局范围内,通常不会有太大的意义。

In your case there must be some code in the main script being executed (where __name__ would be '__main__' unless that script is launched as a module with the -m Python flag) that invokes directly or indirectly your my_funct function.在您的情况下,正在执行的主脚本中必须有一些代码(其中__name__将是'__main__'除非该脚本作为带有-m Python 标志的模块启动)直接或间接调用您的my_funct函数。 It is that code that should be placed in a if __name__ == '__main__': block.正是该代码应该放在if __name__ == '__main__':块中。 For example:例如:

def my_funct
    ##does something
    p1 = Process(target = funct1)
    p2 = Process(target = funct2)
    p1.start()
    p2.start()
    ##more code
    p1.terminate()
    p2.terminate()
    return something

def function main():
    # Do some work
    ...
    # Call my_funct, which creates new child processes:
    print(my_funct())
    # Do some more work
    ...

if __name__ == '__main__':
    # The following function invocation is at global scope in the main script 
    # and invokes code that will ultimately be creating new child processes:
    main()

If the if __name__ == '__main__': test were instead moved to where you had it originally, then function main would be invoked as part of the initialization of memory for the new processes that were created in function my_funct .如果if __name__ == '__main__': test 被移到原来的位置,那么函数main将作为在函数my_funct中创建的新进程的内存初始化的一部分被调用。 But I am sure you would not want main or any part of my_funct to be re-executed, which would happen with this move.但我敢肯定,您不希望my_functmain或任何部分被重新执行,这会随着这一举动而发生。

Note笔记

I should add that any code at global scope that you do not want or do not need to be re-executed as part of memory initialization for the new child process should be placed within a if __name__ == '__main__': block , not just process-creation code.我应该补充一点,您不希望或不需要作为新子进程的内存初始化的一部分在全局范围内重新执行的任何代码都应该放在if __name__ == '__main__': block 中,而不仅仅是进程创建代码。 Also note that if your my_funct is imported from some module, then __name__ would not be '__main__' to begin with.另请注意,如果您的my_funct是从某个模块导入的,那么__name__将不会以'__main__'开头。

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

相关问题 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 如何在另一个 python 脚本中访问函数内的变量 - How can I access a variable inside a function in another python script 如何启动硒测试所需的另一个过程 - How can I start another process required by my Selenium tests 如何启动进程并将其放入python的后台? - How can I start a process and put it to background in python? python multiprocessing中另一个进程的可用结果时如何启动进程 - How to start a process when available result of another process in python multiprocessing 如何在 python 中的另一个 function 中调用 function? - How do I call function inside another function in python? 如何在 Windows 中读取 Python 中另一个进程的内存? - How can I read the memory of another process in Python in Windows? 如何从 Python 中的另一个文件调用 function 中定义的变量? - How can I call a variable defined inside a function from another file in Python? 如何在函数Login()中获取'username'的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program? 我如何在另一个函数中使用一个函数 - How can i use a function inside of another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM