【问题标题】:Python gevent asynchronousPython gevent 异步
【发布时间】:2017-01-30 20:42:06
【问题描述】:

当我使用 gevent 时,它仍然是同步的并且运行时间没有减少。

这是我的脚本:

def fun(i):
    p = subprocess.Popen(['./main', 'data.txt'], stdout=PIPE, stderr=PIPE)
    err = p.communicate()[1]
    p.wait()

def synchronous():
    for i in range(1,10):
        fun(i)

def asynchronous():
    threads = [gevent.spawn(fun, i) for i in xrange(10)]
    gevent.joinall(threads)

比较%timeit synchronous()%timeit asynchronous(),变化不大。而'main'是编译后的c++文件,'data.txt'是'main'的输入文件。

我猜问题是我使用子进程调用外部例程,但我不知道如何解决这个问题。

【问题讨论】:

标签: python python-2.7 asynchronous gevent


【解决方案1】:

p.wait()

这使您的代码同步。

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2012-05-03
    • 2019-10-09
    • 2012-05-27
    相关资源
    最近更新 更多