#π threadpoolexecutor request
53 messages Β· Page 1 of 1 (latest)
@flat tree
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
the per minute part is the hardest part, you'll have to figure something out yourself
aw dang it
You could have one thread put that amount of URLs into a work queue per minute.
But I'm not sure how compatible this is with the ThreadPool API
the per minute part is what I dont know
just call .submit() N times per minute
let the executor figure out how to assign those to whatever thread it wants
how would I make sure its one minute
use time.sleep
fair enough
with executor(workers=x) as executor:
for url in urls:
executor.submit(request, url)
time.sleep(60 / requests_per_minute)
psuedocode, obviously
alright ty π , I'll give it a try
np
wait, wouldnt this only do one a minute?
if requests_per_minute is 6, you'd want to wait 10 seconds between requests
60 / 6 = 10
but then I wouldnt need to do the executor
wait, I dont think I need it
would it just work if I did them one by one
you could just use threading.Thread here, but that API is worse
Well, then you need to factor in how long the request takes
Depends on how high your request limit is and how long the request takes, yeah
i.e. if the request takes 5 seconds then you're limited to 12 requests per minute and cannot go higher
rate limit is 100 per minute, im planning on doing 20 per minute to be safe as I have another hourly programme requesting from it too
all I need it to do is save that data in a database
but if you don't care - here's how you do that:
for url in urls:
before = time.perf_counter()
request(url)
after = time.perf_counter()
request_duration = after - before
ideal_delay = 60 / requests_per_minute
remaining_delay = ideal_delay - request_duration
time.sleep(remaining_delay)
(note that it's possible for remaining_delay to be negative)
what does the threadpool executor do in this case?
It has multiple requests running at the same time
it will START a request every N seconds, even if there are already multiple requests that are in-progress
This example can have multiple requests running in parallel
this cannot
executor.submit(request, url) only STARTS a request, doesn't wait for it to finish
ah okay
I think that would be better
how would I save the data to a variable?
im still kinda new to threading
you can eventually call ```py
future = executor.submit(request, url)
data = future.result()
but calling future.result() will wait for the request to complete (if it hasn't already)
how would I save all of them and get the result after its finished?
with ThreadPoolExecutor(X) as executor:
futures = []
for url in urls:
futures.append(executor.submit(request, url))
time.sleep(60 / requests_per_minute)
for future in futures:
print(future.result())
you could also do ```py
for url, future in zip(urls, futures):
print(url, future.result())
np
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.