#πŸ”’ threadpoolexecutor request

53 messages Β· Page 1 of 1 (latest)

flat tree
#

how would I make a portion of requests per minute from a list of urls using threadpoolexecutor?

oak vortexBOT
#

@flat tree

Python help channel opened

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.

orchid pendant
#

the per minute part is the hardest part, you'll have to figure something out yourself

zinc flame
#

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

flat tree
sturdy crown
#

just call .submit() N times per minute

#

let the executor figure out how to assign those to whatever thread it wants

flat tree
sturdy crown
#

use time.sleep

flat tree
#

fair enough

sturdy crown
#
with executor(workers=x) as executor:
  for url in urls:
    executor.submit(request, url)
    time.sleep(60 / requests_per_minute)
#

psuedocode, obviously

flat tree
#

alright ty πŸ‘ , I'll give it a try

sturdy crown
#

np

flat tree
sturdy crown
#

if requests_per_minute is 6, you'd want to wait 10 seconds between requests

#

60 / 6 = 10

flat tree
#

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

sturdy crown
#

you could just use threading.Thread here, but that API is worse

sturdy crown
zinc flame
sturdy crown
#

i.e. if the request takes 5 seconds then you're limited to 12 requests per minute and cannot go higher

flat tree
#

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

sturdy crown
#

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)

flat tree
sturdy crown
flat tree
#

but wouldnt the example you showed do them one at a time also?

#

or am I tripping

sturdy crown
#

it will START a request every N seconds, even if there are already multiple requests that are in-progress

sturdy crown
flat tree
#

ohh

#

I'll use the other one then

sturdy crown
#

executor.submit(request, url) only STARTS a request, doesn't wait for it to finish

flat tree
#

I think that would be better

flat tree
#

im still kinda new to threading

sturdy crown
#

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)

flat tree
#

how would I save all of them and get the result after its finished?

sturdy crown
#
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())

flat tree
#

interesting

#

thank you!

sturdy crown
#

np

oak vortexBOT
#
Python help channel closed

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.