2016-08-09 5 views
1

다음 두 가지 버전의 코드는 eventlet을 사용합니다. 두 개의 spawn_n 호출이 동시에 실행될 것으로 예상되지만 그 경우는 아닙니다. 실행은 순차적으로 발생합니다.Python Eventlet의 스폰 결과가 동시에 실행되지 않습니다

import eventlet 
import threading 
import datetime 
from eventlet.green import urllib2 


def hello(name): 
    print eventlet.greenthread.getcurrent(), threading.current_thread() 
    print datetime.datetime.now() 
    print " %s hello !!" % name 
    urllib2.urlopen("http://www.google.com/intl/en_ALL/images/logo.gif").read() 

eventlet.spawn(hello, 'abc') 
eventlet.spawn(hello, 'xyz') 
eventlet.sleep(0) 
는 는 O/P

: < _MainThread (MainThread가 시작 140365670881088)>
2016년 8월 9일 14 : 04 : 57.782866
ABC 안녕하십니까! 05 :
< _MainThread>
2016년 8월 9일 14 (MainThread가 시작 140365670881088) 02.929903
XYZ 안녕하세요!

import eventlet 
import threading 
import datetime 
from eventlet.green import urllib2 


def hello(name): 
    print eventlet.greenthread.getcurrent(), threading.current_thread() 
    print datetime.datetime.now() 
    print " %s hello !!" % name 
    urllib2.urlopen("http://www.google.com/intl/en_ALL/images/logo.gif").read() 

pool = eventlet.GreenPool(size=4) 
pool.spawn_n(hello, 'pqr') 
pool.spawn_n(hello, 'lmn') 
pool.waitall() 
는 는 O/P

: 05 : < _MainThread>
2016년 8월 9일 14 (MainThread가 시작 139,897,149,990,720) 25.613546
PQR 안녕하십니까!
< _MainThread (MainThread가 시작 139,897,149,990,720)>
2016년 8월 9일 14 : 05 : 30.699473
LMN 안녕하십니까!

두 버전의 코드에서 호출은 순차적으로 발생합니다.

+0

내가 다른 eventlet.spawn_n (안녕하세요, 'DEF') 또는 poll.spawn_n를 추가 한 후 (안녕하세요, '처음'). 마지막 2 번의 호출은 몇 밀리 초의 차이로 발생합니다. 문서화 시점에서, eventlet은 순전히 동시 적이 아닙니다. 어쨌든 매우 적은 수정으로 실제 동시 실행 방식이 있습니까? – nebi

+0

'concurrently'를 정의하고 '순차적'과 어떻게 다른지 정의하십시오. – temoto

답변

0
def fun(tag): 
    print('{0} begin'.format(tag)) 
    eventlet.sleep(0.1) # pretty much the same as running HTTP request and throwing results away 
    print('{0} end'.format(tag)) 

t1 = time.time() 
pool.spawn(fun, 'Turtle') 
pool.spawn(fun, 'Achilles') 
pool.waitall() 
tt = time.time() - t1 
print('Total time: {0:.2f}'.format(tt)) 

그리고 당신은 다음과 유사한 출력을 볼 것입니다 :

Turtle begin 
Achilles begin 
Turtle end 
Achilles end 
Total time: 0.10 

기능의 실행이 협력 동시성의 정의에 가까운 IO 대기 포인트 (수면)에서 인터리브 된 보여줍니다. 또한 총 시간은 두 개의 수면 시간의 합계가 아니라 최대 + 인프라 오버 헤드 (이 합성 테스트에서는 거의 가깝지 않아야 함)에 주목하십시오.

입출 력 전에 인쇄본 (이벤트 릿에서 IO 대기를 생성하지 않음)과 이후 인쇄물 만 있기 때문에 문제의 코드가 표시되지 않았습니다. 그래서 당신이 실제로 실행 한 것은이 :

main: pool.waitall() begin, switch to Eventlet loop 
loop: oh, there are greenthreads scheduled to run now, switch to 1 
1: print this 
1: print that 
1: urllib... start network IO, switch to loop 
loop: there is another green thread scheduled, switch to 2 
2: print this 
2: print that 
2: urllib... start network IO, switch to Eventlet loop 
loop: wait for any event that should wake greenthread, 
    suppose that would be thread 1 urllib that is finished first 
1: HTTP request finished, thread 1 finished, but no code to prove it 
loop: wait for any event... 
2: HTTP request finished, thread 2 finished 
main: pool.waitall() finished