YoungRexroth
YoungRexroth

Reputation: 69

How do I call a function repeatedly?

By means of the module multiproproccessing.dummy, I want to perform the function getData() 10 times. However, the print function is only called once.

from multiprocessing.dummy import Pool as ThreadPool

print("Begin")

import itertools
import urllib.request
import time

def getData():
        print("Do something...")

t0 = time.time()
with ThreadPool(10) as pool:
    results = pool.map_async(getData(), iterable=(x for x in range(10)))
    
t1 = time.time()
totalTime = t1-t0
print ("Total Cyclic time: ", totalTime)   

Upvotes: 1

Views: 54

Answers (1)

wakey
wakey

Reputation: 2409

You're calling getData() before passing it into map_async, which passes in its results, instead of passing in the function itself.

Change getData() to getData and you should be OK.

    results = pool.map_async(getData, ...)

Upvotes: 3

Related Questions