简体   繁体   中英

Run 3 python functions concurrently then add their values together

I am generating the coefficients for x, y and z. I want to run the generating functions simultaneously (as they take a while), then once all generated, merge the coefficients together. My ideal code will be a bit like this:

#generated in parallel
  x = generate_x()
  y = generate_y()
  z = generate_z()

merged = x + y + z

I don't want to wait for each generate function before I start generating the next one, but I also need them all to finish before I merge them at the end. How can this be done?

You can use the "concurrent" library to easily implement concurrency in your code.

from concurrent import futures

with futures.ThreadPoolExecutor() as exe:
    results = [exe.submit(generate_x), exe.submit(generate_y), exe.submit(generate_z)]
    sum_coeffs = sum(result.result() for result in futures.as_completed(results))

You can store all threads in a list like this while they are running and then access the thread when it's completed by using the as_completed() method in the futures library. After that access the stored result in the completed thread with the result() method and sum them all up.

Look here to learn more about the library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM