不执行任何类函数python2.7.13的多处理

2024-10-05 19:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用multiprocessing.Pool异步运行类函数。但似乎我不能调用pool.apply_async()中的任何类函数。不过,在同一个文件中定义的函数也可以。你知道吗

这是已知的行为,我错过了它,如果是的话,我可以做什么以尽可能少的代码异步运行我的函数?你知道吗

以下是我的两个文件,演示了这个问题:

## test.py ##
import multiprocessing as mp
from testclass import testclass

def nonclass_test():
    print("nonclass_test")

def nonclass_callback(self):
    print("nonclass_callback")


pool = mp.Pool()

test = testclass()

#test.test()
#print("testclass working ...")

#pool.apply_async(nonclass_test, args = (), callback = nonclass_callback)
pool.apply_async(test.test, args = (), callback = nonclass_callback)
pool.apply_async(test.test, args = (test, ), callback = test.callback)

pool.close()
pool.join()

---

## testclass.py ##
class testclass:
    def __init__(self):
        print("initiated")

    def test(self):
        print("classtest")

    def callback(self):
        print("classcallback")

在我的输出中,我只得到消息"nonclass_test""nonclass_callback"。似乎类函数(既不是回调函数,也不是实际函数)正在执行。你知道吗

已经谢谢你的帮助了。你知道吗


Tags: 文件函数testselfasyncdefcallbackargs