ich will ca. 20 urls gleichzeitig ansprechen und nach verfügbarkeiten vo produkten fragen (ähnlich preisvergleich seiten, die zig shops absuchen)
jetzt hatte ich mir vorgestellt das mit threads zu machen, lese aber folgendes in der mailing list von python
was heisst "they execute one at the time" ? das macht ja mal gar keinen sinn dann.Because of the GIL, thread pools are not as useful in Python as you
might expect -- they execute one at a time and do not take advantage of
hyper-threading or multiple processors. If that kind of efficiency is
what you are after, then take a look at PyLinda which can coordinate
communication between multiple instances of Python running in separate
threads
im moment starte ich gleich viele threads wie ich urls habe, nach diesem beispiel von gerold:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import urllib2
import threading
import time
lock = threading.Lock()
def download(url, retvals):
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError, exception:
html = "ERROR: %s" % str(exception)
lock.acquire()
retvals.append((url, html))
lock.release()
def main():
urls = (
"http://gerold.bcom.at/",
"http://halvar.at/",
"http://lugt.at/",
"http://microsoft.de/",
"http://www.python-forum.de/",
"http://python.org/",
"http://wallerforum.com/",
#"http://log-in.fachdid.fu-berlin.de/Archiv/2001/2/Thema/", # braucht zu lange
)
retvals = []
threads = []
for url in urls:
threads.append(
threading.Thread(
target = download, kwargs = dict(url = url, retvals = retvals)
)
)
threads[-1].start()
time.sleep(0.2)
# Warten bis alle fertig sind
for thread in threads:
thread.join()
for url, html in retvals:
print url
if html.startswith("ERROR"):
print "Achtung Fehler!"
print html[:100]
print
if __name__ == "__main__":
main()