따라서 두 가지 문제가 있으며 한 가지 예를 들어 설명하겠습니다. pycurl이 이미 멀티 스레딩을 수행했거나 한 번에 한 번에 열심히하지 않았 음을 주목하십시오.
#! /usr/bin/env python
import sys, select, time
import pycurl,StringIO
c1 = pycurl.Curl()
c2 = pycurl.Curl()
c3 = pycurl.Curl()
c1.setopt(c1.URL, "http://www.python.org")
c2.setopt(c2.URL, "http://curl.haxx.se")
c3.setopt(c3.URL, "http://slashdot.org")
s1 = StringIO.StringIO()
s2 = StringIO.StringIO()
s3 = StringIO.StringIO()
c1.setopt(c1.WRITEFUNCTION, s1.write)
c2.setopt(c2.WRITEFUNCTION, s2.write)
c3.setopt(c3.WRITEFUNCTION, s3.write)
m = pycurl.CurlMulti()
m.add_handle(c1)
m.add_handle(c2)
m.add_handle(c3)
# Number of seconds to wait for a timeout to happen
SELECT_TIMEOUT = 1.0
# Stir the state machine into action
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Keep going until all the connections have terminated
while num_handles:
# The select method uses fdset internally to determine which file descriptors
# to check.
m.select(SELECT_TIMEOUT)
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Cleanup
m.remove_handle(c3)
m.remove_handle(c2)
m.remove_handle(c1)
m.close()
c1.close()
c2.close()
c3.close()
print "http://www.python.org is ",s1.getvalue()
print "http://curl.haxx.se is ",s2.getvalue()
print "http://slashdot.org is ",s3.getvalue()
마지막으로,이 코드는 주로 pycurl 사이트에 대한 예제를 기반으로 =. =
당신이 정말로 문서를 읽어야 할 수있다. ppl은 그것에 막대한 시간을 소비합니다.
'class Spider' - 아주 멋지다! –
감사합니다. Corey; 어떻게해야합니까 (초보자에게) 모두 완료 될 때까지 기다려야합니까? – denis
Denis, main()의 각 스레드에서 join()을 호출 할 수 있습니다. 스레드가 완료 될 때까지 차단됩니다. –