from multiprocessing.dummy import Pool as ThreadPool
import multiprocessing as mp
def func(a):
pthData = "C:/temp/temp.txt"
with open(pthData, 'r') as file:
done = file.read().splitlines()
if a in done:
return 'done'
q.put(a)
return a
def listener(q):
pthData = "C:/temp/temp.txt"
m = q.get()
with open(pthData, 'a') as the_file:
the_file.write( m + '\n')
#he_file.write(str(m) + '\n')
a = ['a', 'b', 'c', 'd', 'a', 'b']
# Make the Pool of workers
pool = ThreadPool(4)
#must use Manager queue here, or will not work
manager = mp.Manager()
q = manager.Queue()
#put listener to work first
watcher = pool.apply_async(listener, (q,))
pool.starmap(func, a, q)
## TypeError: '<=' not supported between instances of 'AutoProxy[Queue]' and 'int'
pool.starmap(func, a)
## Runs but only writes 'a' to temp file
pool.starmap(func, (a, q))
## func() takes 1 positional argument but 6 were given
pool.apply_async(func, (a, q))
## freezes on pool.join
# Close the pool and wait for the work to finish
pool.close()
pool.join()
Why is the apply_async freezing on the pool.join()? I tried putting it into a if name == ' main ' but it had the same result.
How do I properly call func
passing 1 argument (a) and the queue (q)?
How do I properly call func passing 1 argument (a) and the queue (q)?
This at-least does not freeze :
temp.txt
exists before execution.q
parameter to func
. def func(a,q):
print(f'func({a})')
...
apply_async
in a list comprehension. if __name__ == '__main__':
# Make the Pool of workers
with ThreadPool(4) as pool:
q = queue.Queue()
#put listener to work first
watcher = pool.apply_async(listener, (q,))
results = [pool.apply_async(func, (item, q)) for item in a]
# just check stuff
for result in results:
result.wait()
print(result, result.successful(),result.get())
pool.close()
pool.join()
listener
running once then stopping.apply_async
because it was one of the options in your question.python threading producer consumer site:stackoverflow.com
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.