[英]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_funct
的main
或任何部分被重新执行,这会随着这一举动而发生。
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.