【问题标题】:how to make a process sleep without making other processes sleep in python multiprocessing如何在python多处理中使进程睡眠而不使其他进程睡眠
【发布时间】:2019-12-23 22:49:00
【问题描述】:

当使用模块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

【问题讨论】:

标签: python csv multiprocessing


【解决方案1】:

用途:

time.sleep( delay )

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2015-09-20
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2011-01-16
    相关资源
    最近更新 更多