[英]how to make a process sleep without making other processes sleep in python multiprocessing
How do I make one thread sleep without making other threads sleep in Python, when using the module multiprocessing
?使用模块multiprocessing
如何在 Python 中让一个线程休眠而不让其他线程休眠? I want to make getDataProcess sleep for some time when the queue has less than 10 elements.当队列少于 10 个元素时,我想让 getDataProcess 休眠一段时间。 During this time the main thread will populate the queue with more data and the while loop will continue executing.在此期间,主线程将使用更多数据填充队列,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
Use:用:
time.sleep( delay )
It will make your process sleep without affecting others.它会让你的进程休眠而不影响其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.