是否可以从 Process 对象取回目标?
Is it possible to get the target back from a Process object?
我有一个 multiprocessing.Process
对象。在构造过程中,我们在其中传递了 target
和 args
。是否有可能取回这两个值?或者至少是关于当前 Process 对象正在使用的目标的某种提示?
是的,这是可能的,尽管我们不应该这样做(从 API 设计者决定使用下划线开头的属性可以推断出这一点)。
from multiprocessing import Process
def foo(x):
print(x)
p = Process(target=foo, args=(1,))
print(p._target)
# <function foo at 0x000002457042B158>
print(p._args)
# (1,)
p._target(*p._args)
# 1
我有一个 multiprocessing.Process
对象。在构造过程中,我们在其中传递了 target
和 args
。是否有可能取回这两个值?或者至少是关于当前 Process 对象正在使用的目标的某种提示?
是的,这是可能的,尽管我们不应该这样做(从 API 设计者决定使用下划线开头的属性可以推断出这一点)。
from multiprocessing import Process
def foo(x):
print(x)
p = Process(target=foo, args=(1,))
print(p._target)
# <function foo at 0x000002457042B158>
print(p._args)
# (1,)
p._target(*p._args)
# 1