Problem using greenlet to perform multiple functions simultaneously

The following script aims to execute many functions at the same time, but I have no idea why it is not working correctly.
Functions are performed sequentially, and not in parallel.

I would appreciate any suggestion to clarify to me what I am doing wrong.

import time
import eventlet


EXECUTION_TIMEOUT = 10

def example(name, steps_limit):
    print 'Starting process %s with %d steps' % (name, steps_limit)
    for i in range(1, steps_limit+1):
        print "Process %s, step %d" % (name, i)
        time.sleep(2)
    print 'Finishing process %s with %d steps' % (name, steps_limit)


def fetch(input_data):
    example(input_data['name'], input_data['steps'])

test_data = [{'name':'proceso1', 'steps':3},
             {'name':'proceso2', 'steps':5},
             {'name':'proceso3', 'steps':6},
             ]

def main():
    #Setting up time out
    timeout = eventlet.timeout.Timeout(EXECUTION_TIMEOUT)
    #initialize pool
    pool = eventlet.GreenPool(size=1000)
    try:
        for hits in pool.imap(fetch, test_data):
            pass
    except eventlet.Timeout:
        print 'Operation interrupted by timeout concept'
    finally:
        timeout.cancel()


if __name__ == '__main__':
    main()
+5
source share
1 answer

You are right in time.sleep()being the culprit.

But you do not need to replace time.sleep()with eventlet.sleep(). Instead, you can time.sleepbecome a monkey patch eventlet.sleep.

, , , , , time.sleep, , , .

gevent gevent.monkey.patch_all() . eventlet .

+2

All Articles