1

Ok, say I have this code:

import time

def helloworld(sleep_time):
    while True:
        time.sleep(sleep_time)
        print('Hello world!')

def hellocountry():
    while True:
        time.sleep(60)
        print('Hello country!')

if __name__ == '__main__':
    with Pool(3) as p:
        p.map(helloworld, [1, 5, 7])

How would I execute hellocountry, while helloworld is being executed? I guess I could write a wrapper function, but that seems rather clumsy and unpythonic.

3
  • use .submit() (I suggest for the helloworld() calls too).
    – Elazar
    Oct 9, 2017 at 20:43
  • So is Pool() just a wrapper around ThreadPoolExecutor? Oct 9, 2017 at 21:07
  • In your code, Pool is undefined. But no, I confused Pool and Executor.
    – Elazar
    Oct 10, 2017 at 12:24

1 Answer 1

1

Just use the apply_async method.

if __name__ == '__main__':
    with Pool(3) as p:
        p.apply_async(hellocountry)
        p.map(helloworld, [1, 5, 7])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.