0

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.

insomnia
  • 191
  • 2
  • 12
  • Try using `pool.map` something like this. http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments – lennard Sep 22 '16 at 10:10
  • @lennard It seems work. Thanks a lot. But what's the difference between these two ways? – insomnia Sep 22 '16 at 10:22

1 Answers1

0

p.wait()

This is making your code synchronous.

eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20