在Python2.7中应用异步

问题描述 投票:0回答:1

我试图编写用于多处理的简单代码,该代码以前在Python3中可用。目前,我想将我的代码从Python3.6迁移到Python2.7。在Python3.6中,它显示了预期的结果,但在Python 2.7中则没有。有人说我需要使用with mp.Pool() as pool,但是结果是一样的。这是我的代码:

from __future__ import print_function
from multiprocessing import Pool

class Try():
    def print_this(self, test):
        print(test)

x = Try()
pool = Pool(1)
for i in range(10):
    pool.apply_async(x.print_this, args=(i,))
pool.close()
pool.join()

Python3将显示此内容>

0
1
2
3
4
5
6
7
8
9

但不是在Python2中。你有什么建议吗?谢谢。

我试图编写用于多处理的简单代码,该代码以前在Python3中可用。目前,我想将我的代码从Python3.6迁移到Python2.7。在Python3.6中,它显示了预期的结果,但是...

python python-2.7 asynchronous multiprocessing pool
1个回答
0
投票

[以前,我看到一个回答说我需要使用ThreadPool作为替代。我不知道为什么他/她删除了答案,因为它实际上效果很好。所以,这是我的代码:

© www.soinside.com 2019 - 2024. All rights reserved.