0

I have 3 functions from which I need to get the values to use them in the main() function. Functions are executed in turn and it takes a lot of time. How can I execute the first three functions at the same time and get values ​​from them?

def session_ID():
    ...
    driver.get('url')
    sessionID = driver.execute_script('return ...')
    return sessionID

def reCaptcha():
    ...
    recaptcha = requests.get('url')
    return recaptcha

def hCaptcha():
    ...
    hcaptcha = requests.get('url')
    return hcaptcha

def main():
    sessionID = session_ID()
    recaptcha = reCaptcha()
    hcaptcha = hCaptcha()
    print(sessionID, recaptcha, hcaptcha)
  • 1
    I see you have added `multiprocessing` tags. Have you tried that? – quamrana Jan 07 '23 at 17:09
  • I'm just learning python, and other people's examples seem complicated to me. I hope for help to understand this with my own example. – Katerina_GG Jan 07 '23 at 17:13
  • Please update your question with some other example which seems complicated to you. – quamrana Jan 07 '23 at 17:17
  • I would like to understand this with my own example. My head can't take it anymore) – Katerina_GG Jan 07 '23 at 18:07
  • 1
    Either multiprocessing or multithreading would be the way to go depending on what your 3 functions are doing. If they're CPU bound then go for the former; otherwise the latter. Take a look at the concurrent.futures module – DarkKnight Jan 07 '23 at 18:07
  • Just add the examples. We don't know what you are looking at and we don't know what seems complicated to you. (Yes, multiprocessing and multithreading are complicated, but its all relative). – quamrana Jan 07 '23 at 18:09
  • I edited my code a little, I think it will be easier to understand what I need. – Katerina_GG Jan 07 '23 at 18:24
  • Does [How can I use threading in Python?](/questions/2846653) answer your question? – Karl Knechtel Jan 07 '23 at 18:38

1 Answers1

1

Here is a multithreading pattern that you might be able to use.

Each function that is required to run in its own thread (asynchronously) returns its own name and some value. Build a dictionary keyed on the function names from which you can subsequently and very easily access the returned values.

You will need to make significant adjustments to this code if your threaded functions require parameters.

For example:

from multiprocessing.pool import ThreadPool as TP

def session_ID(me):
    return me, 99

def reCaptcha(me):
    return me, 'banana'

def hCaptcha(me):
    return me, 'apple'

with TP() as pool:
    d = {}
    for result in [pool.apply_async(func, [func.__name__]) for func in (session_ID, reCaptcha, hCaptcha)]:
        k, v = result.get()
        d[k] = v
        
print(d)

Output:

{'session_ID': 99, 'reCaptcha': 'banana', 'hCaptcha': 'apple'}
DarkKnight
  • 19,739
  • 3
  • 6
  • 22