Home > Back-end >  How to execute functions in Python at the same time and get a values from them?
How to execute functions in Python at the same time and get a values from them?

Time:01-08

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)

CodePudding user response:

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'}
  • Related