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()
Почему apply_asyn c зависает в pool.join ()? Я попытался поместить его в if name == ' main ', но результат был тот же.
Как правильно вызвать func
, передав 1 аргумент (а) и очередь (q)?