Ich habe einen unendlichen multithreaded Downloader zu blockieren, die wie folgt funktioniert:Python: Multihreading Downloader sich
queue = Queue() # links for downloading
def downloader(queue):
while:
link = queue.get()
# do download with requests library
for i in range(4):
task = Thread(target=downloader, args=(queue,))
task.start()
Queue regelmäßig mit neuen Daten gefüllt ist. Aber wenn ich drucken, welcher Link heruntergeladen wurde, sieht es aus wie Fäden arbeiten nicht parallel, zum Beispiel:
time 0 - thread 1 - link 1
time 1 - thread 1 - link 2
time 2 - thread 1 - link 3
time 3 - thread 1 - link 4
time 4 - thread 2 - link 5
time 5 - thread 2 - link 6
time 6 - thread 2 - link 7
time 7 - thread 3 - link 8
time 8 - thread 3 - link 9
time 9 - thread 3 - link 10
time 10 - thread 1 - link 11
time 11 - thread 1 - link 12
Ich hoffte, dass es wird mehr wie: Thema 1 2 3 4 1 2 3 4. Server hat 4 cpus.
Dank
Können Sie den Anforderungscode anzeigen? – Eloims
Wie messen Sie Ihre Zeit? Mit welcher Frequenz fügen Sie die Links in die 'Queue' ein? Wie viel Zeit braucht der Downloader? (Ist es möglich, dass jede Ausführung von 'Downloader' abgeschlossen wird, bevor _ der nächste Link in die 'Queue' eingefügt wird?) Beachten Sie auch, dass Multithreading nicht notwendigerweise alle CPUs verwendet. Sie haben auch Multithreading auf einer einzigen CPU. –
Kurz gesagt: 'session = requests.Session()' 'session.cookies = login_cookies.copy()' 'für Link in Links:' 'data = session.get (url, params = {... }) '' wenn data.status_code == 200: '' fp = open (filepath, 'wb') '' fp.write (data.content) '' fp.close() ' – Greno