Answer the question
In order to leave comments, you need to log in
Return value from stream in python 3?
Actually a question. Threads in python work smartly, but I didn’t find how to normally return a value from it. Here is an example code:
import requests
import random
import re
import queue
pattern = 'http://www.astateoftrance.com/episodes/episode-'
k = 700
lock = threading.Lock()
def getPage():
#print ("Hello")
global k
while (k < 800):
url = pattern+str(k)+'/'
response = requests.get(url)
content = response.content.decode('utf-8')
contLow = content.lower()
#print(contLow)
if (re.findall('', contLow)):
print(url)
lock.acquire()
k = k + 1
lock.release()
threads = []
for i in range(20):
t = threading.Thread(target=getPage)
threads.append(t)
t.start()
Answer the question
In order to leave comments, you need to log in
Threads in python work smartly
Usually, streams do not return values, but write them to a list or somewhere else.
I would write your example like this (by the way, all downloaded pages are in the results):
from multiprocessing.dummy import Pool as ThreadPool
from pprint import pprint
import requests
pattern = 'http://www.astateoftrance.com/episodes/episode-'
start_url = 700
urls_list = []
def gen_urls(start):
for i in range(start, 800):
url = pattern + str(i) + '/'
urls_list.append(url)
def my_url_get(url):
result = requests.get(url)
print("{url} was Downloaded".format(url=url))
return result
gen_urls(start_url)
pprint(urls_list)
pool = ThreadPool(20)
results = pool.map(my_url_get, urls_list)
pool.close()
pool.join()
pprint(results[0].content.decode('utf-8'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question