Gevent函数没有异步运行

2024-04-19 23:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

this Gevent tutorial中,它讨论了如何生成多个线程以异步运行。你知道吗

import gevent
import random

def task(pid):
    """
    Some non-deterministic task
    """
    gevent.sleep(random.randint(0,2)*0.001)
    print('Task %s done' % pid)

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

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

print('Synchronous:')
synchronous()

print('Asynchronous:')
asynchronous()

结果

Synchronous:
Task 1 done
Task 2 done
Task 3 done
Task 4 done
Task 5 done
Task 6 done
Task 7 done
Task 8 done
Task 9 done
Asynchronous:
Task 1 done
Task 5 done
Task 6 done
Task 2 done
Task 4 done
Task 7 done
Task 8 done
Task 9 done
Task 0 done
Task 3 done

这和我预期的一样。然而,当我用一个“工作”任务来代替琐碎的睡眠任务时,它的工作方式就不一样了。。。你知道吗

import gevent
import random

def task(pid):
    start_time = time.time()
    array = []
    for x in range(0, 1000000):
        x = str(x) + "WasteMyTime"
        array.append(x)
    elapsed_time = time.time() - start_time
    print("Task " + str(pid) + " took  " + str(elapsed_time))

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

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

print('Synchronous:')
synchronous()

print('Asynchronous:')
asynchronous()

结果

Synchronous:
Task 1 took  0.3065943717956543
Task 2 took  0.2897024154663086
Task 3 took  0.29267001152038574
Task 4 took  0.2936718463897705
Task 5 took  0.28526878356933594
Task 6 took  0.29134082794189453
Task 7 took  0.28323960304260254
Task 8 took  0.28522467613220215
Task 9 took  0.28423142433166504
Asynchronous:
Task 0 took  0.2896885871887207
Task 1 took  0.2837369441986084
Task 2 took  0.28224802017211914
Task 3 took  0.2857201099395752
Task 4 took  0.28621697425842285
Task 5 took  0.28621697425842285
Task 6 took  0.28621602058410645
Task 7 took  0.2857208251953125
Task 8 took  0.28720879554748535
Task 9 took  0.2847275733947754

异步任务一个接一个地慢慢打印出来,整个异步块的时间和同步块的时间一样长。这是用户错误吗?你知道吗


Tags: inimportfortasktimedefgeventrange