python中的多线程进程使用队列写入文件检查工作是否已完成 从multiprocessing.dummy导入池作为线程池 将多处理作为mp导入 def func(a): pthData=“C:/temp/temp.txt” 打开(pthData,'r')作为文件: 完成=file.read().splitlines() 如果完成了以下步骤: 返回“完成” q、 付诸表决(a) 归还 def侦听器(q): pthData=“C:/temp/temp.txt” m=q.get() 以open(pthData,'a')作为_文件: _文件.write(m+'\n') #写入(str(m)+'\n') a=['a','b','c','d','a','b'] #使工人聚集在一起 池=线程池(4) #必须在此处使用管理器队列,否则将无法工作 manager=mp.manager() q=manager.Queue() #首先让听者工作 watcher=pool.apply\u async(侦听器,(q,)) pool.starmap(func,a,q) ##TypeError:'

python中的多线程进程使用队列写入文件检查工作是否已完成 从multiprocessing.dummy导入池作为线程池 将多处理作为mp导入 def func(a): pthData=“C:/temp/temp.txt” 打开(pthData,'r')作为文件: 完成=file.read().splitlines() 如果完成了以下步骤: 返回“完成” q、 付诸表决(a) 归还 def侦听器(q): pthData=“C:/temp/temp.txt” m=q.get() 以open(pthData,'a')作为_文件: _文件.write(m+'\n') #写入(str(m)+'\n') a=['a','b','c','d','a','b'] #使工人聚集在一起 池=线程池(4) #必须在此处使用管理器队列,否则将无法工作 manager=mp.manager() q=manager.Queue() #首先让听者工作 watcher=pool.apply\u async(侦听器,(q,)) pool.starmap(func,a,q) ##TypeError:',python,multithreading,multiprocessing,Python,Multithreading,Multiprocessing,如何正确调用func传递1参数(a)和队列(q) 这至少不会冻结: 确保执行前存在temp.txt 将q参数添加到func 在列表中使用apply\u async 您将需要解决一些其他问题,如listener运行一次然后停止 要做到这一点,我还使用了许多其他方法,我使用了apply\u async,因为这是您问题中的选项之一 我喜欢自己使用并发的.futures 您可以使用python threading producer consumer site:stackoverflow.co

如何正确调用func传递1参数(a)和队列(q)

这至少不会冻结:

  • 确保执行前存在
    temp.txt
  • q
    参数添加到
    func
  • 在列表中使用
    apply\u async

  • 您将需要解决一些其他问题,如
    listener
    运行一次然后停止
  • 要做到这一点,我还使用了许多其他方法,我使用了
    apply\u async
    ,因为这是您问题中的选项之一
  • 我喜欢自己使用并发的.futures
  • 您可以使用python threading producer consumer site:stackoverflow.com

为什么需要使用星图?我不需要。我在
apply\u async
无效后尝试了它。是否要将列表中的单个项目传递给func,并让func将不在文件中的项目放入队列?您希望/需要侦听器对队列项目执行操作-与func分开?是的,完全正确。然后从队列将它们写入文件。执行之前是否存在
temp.txt
?是否将
a、b、c、d
写入temp.txt?对我来说,它只收到了四分之一的信file@R.M. ... <代码>侦听器运行一次,然后退出-因此它只写第一个字母。不知何故,您需要
listener
不断检查队列,直到不再需要为止。
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()
      def func(a,q):
          print(f'func({a})')
          ...
    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()