简体   繁体   中英

Python gevent asynchronous

When I use gevent and it's still synchronous and run time doesn't decrease.

Here is my script:

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)

By comparing %timeit synchronous() and %timeit asynchronous() , there is little change. And the 'main' is the compiled c++ file and the 'data.txt' is the input file for 'main'.

I guess the problem is that I use subprocess to call a external routine, but I don't know how to solve this problem.

p.wait()

This is making your code synchronous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM