python - 如何在Python多处理中使进程休眠而不使其他进程休眠

标签 python csv multiprocessing

使用模块多处理时,如何在Python中使一个线程休眠而不使其他线程休眠?当队列元素少于 10 个时,我想让 getDataProcess 休眠一段时间。在此期间,主线程将向队列中填充更多数据,并且 while 循环将继续执行。

import multiprocessing as mp
import os
import csv
import sys
import subprocess
import time
cur=[]
dataList=[]
def getData(queue):
    global dataList
    print('get data started')
    with open('temp.csv','w+') as file:
        writer=csv.DictWriter(file,fieldnames=['index','name','fileaccess'],lineterminator='\n')
        while not queue.empty():
            cur=queue.get()
            writer.writerow(cur)
            if len(dataList)<=100:
                dataList.append(cur)
                print('appending to datalist',end='\r')
            if len(dataList)==100:
                showData(queue)
            if(queue.qsize()<10):
                print('danger race condition'+str(queue.qsize()))
                if os.path.exists('temp.csv'):
                    try:
                        os.rename('temp.csv','temp.csv')
                        print('may have completed reading')
                    except OSError as e:
                        #time.sleep(10)
                        print('sleeping to prevent end ')

def showData(queue):
    print('showdata started')
    global dataList

    #time.sleep(1)

    print(dataList)
    if(queue.qsize()<100):
        print('danger race condition')
if __name__=="__main__":
    try:
        filename=sys.argv[1]
        key=sys.argv[2]
    except:
        print('arguments not provided')
    queue = mp.Queue() 
    getDataProcess=mp.Process(target=getData,args=(queue,))
    getDataProcessStatus=False
    showDataProcess=mp.Process(target=showData,args=(queue,))
    showDataProcessStatus=False
    with open('archive/data.csv') as file:
        matches=0
        reader=csv.DictReader(file,fieldnames=['index','name','fileaccess'],delimiter=',')
        header=next(reader)
        for i,row in enumerate(reader):
            if(row['fileaccess'][0]=='d'):
                matches+=1
                queue.put(row)
                if(getDataProcessStatus==False):
                        getDataProcess.start()
                        getDataProcessStatus=True
                        print('getdata started')
                # if(matches>200):
                #     if(showDataProcessStatus==False):
                #         print('show data started')
                #         showDataProcess.start()
                #         showDataProcessStatus=True

最佳答案

用途:

time.sleep( delay )

它会让你的进程休眠而不影响其他进程。

关于python - 如何在Python多处理中使进程休眠而不使其他进程休眠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462051/

相关文章:

python - 基于生成器的协程看似无限递归

python - xarray 可以实现面积积分和面积加权平均值吗?

python - 无法使用 kubernetes python 客户端创建 CRD

python - 在 Python 中找到英文维基百科两篇文章之间的最短路径

python - 在 iter 上并行化循环

mysql - 将 CSV 数据导入到单独的 MYSQL 表中

python - 使用Pandas将CSV读取到具有不同行长的dataFrame中

python - 如何处理 CSV 字典中的 'missing key values' 并通过 Pandas 数据框进行操作?

python - 如何正确使用threading.local()来传递值?

multithreading - 如何在没有竞争条件的情况下在 Erlang 中按需启动 gen_server 或 gen_fsm?