[英]Simultaneous write to multiple files
我正在做一個項目,涉及使用Beaglebone從多個傳感器讀取數據,然后將數據傳遞到文本文件中。我正在監視六種不同的肌肉,因此每個文件都有對應的文件。
現在,有6個傳感器饋入Beaglebone Black的ADC引腳,我的代碼指示Beaglebone為每個引腳創建1個單獨的文件。 每當我運行以下代碼時,我只會得到一個文件(第一個函數運行)。 以前,我沒有包括“ while True:”語句,該語句導致1-2個讀數和六個文件的創建。 我添加了“ while True:”來連續記錄傳感器的數據,因為這是我認為每個文件中的點數不超過2的原因。
我的問題是:是否可以同時寫入多個文件? 我也可以將所有這些數據寫到同一文件中,但是我想知道是什么使該代碼無法正常工作(6個文件)。
#File save for left hamstring
def LeftHamAcquisition():
HamLData = open('HamLeft' + '.txt', 'a+')
file_name = os.path.join(/relevant file path/)
while True:
EMGhamL = ADC.read_raw('AIN1')
HamLData.write(str(elapsed_milliseconds))
HamLData.write('\t')
HamLData.write(str(EMGhamL))
HamLData.write('\n')
#File save for right hams
def RighHamAcquisition():
HamRData = open('HamRight' + '.txt', 'a+')
file_name2 = os.path.join(/relevant file path/)
while True:
EMGhamR = ADC.read_raw('AIN2')
HamRData.write(str(elapsed_milliseconds))
HamRData.write('\t')
HamRData.write(str(EMGhamR))
HamRData.write('\n')
#file save for left quad
def LeftQuadAcquisition():
QuadLData = open('QuadLeft' + '.txt', 'a+')
file_name3 = os.path.join(/relevant file path/)
while True:
EMGquadL = ADC.read_raw('AIN3')
QuadLData.write(str(elapsed_milliseconds))
QuadLData.write('\t')
QuadLData.write(str(EMGquadL))
QuadLData.write('\n')
#file save for right quad
def RightQuadAcquisition():
QuadRData = open('QuadRight' + '.txt', 'a+')
file_name4 = os.path.join(/relevant file path/)
while True:
EMGquadR = ADC.read_raw('AIN4')
QuadRData.write(str(elapsed_milliseconds))
QuadRData.write('\t')
QuadRData.write(str(EMGquadR))
QuadRData.write('\n')
#file save for left vast
def LeftVastAcquisition():
VastLData = open('VastLeft' + '.txt', 'a+')
file_name5 = os.path.join(/relevant file path/)
while True:
EMGVastL = ADC.read_raw('AIN5')
VastLData.write(str(elapsed_milliseconds))
VastLData.write('\t')
VastLData.write(str(EMGVastL))
VastLData.write('\n')
#file save for right vast
def RightVastAcquisition():
VastRData = open('VastRight' + '.txt', 'a+')
file_name6 = os.path.join(/relevant file path/)
while True:
EMGVastR = ADC.read_raw('AIN6')
VastRData.write(str(elapsed_milliseconds))
VastRData.write('\t')
VastRData.write(str(EMGVastR))
VastRData.write('\n')
#The Program
print "Press ctrl-C to end acquisition"
LeftHamAcquisition()
RighHamAcquisition()
LeftVastAcquisition()
RightVastAcquisition()
LeftQuadAcquisition()
RightQuadAcquisition()
try:
pass
except KeyboardInterrupt:
raise data.close()
您的函數調用中包含無限循環,因此它們將永遠不會返回。 一個文件是由LeftHamAcquisition
創建的,但是由於從未返回,因此從未執行過任何其他功能。 您需要使用諸如多處理模塊之類的東西來使它們並行運行。 特別是,我建議使用多處理池和apply_async函數:
import multiprocessing
import Queue
import time
# one global constant: the poison pill
# this could really be whatever you want - my string choice is arbitrary
STOP = "stop"
# change the signature of your function to accept a queue for the main
# process to pass a poison pill
def LeftHamAcquisition(kill_queue):
f_name = 'HamLeft.txt'
# you aren't doing anything with "file_name" - should it be removed?
# file_name = os.path.join(/relevant file path/)
# use file context managers:
with open(fname, 'a+') as HamLData:
while True:
# in the infinite loop, we add a check for the poison pill
try:
val = kill_queue.get(block=False)
if val = STOP:
return # leave if the poison pill was sent
except Queue.Empty:
pass # ignore empty queue
EMGhamL = ADC.read_raw('AIN1')
HamLData.write(str(elapsed_milliseconds))
HamLData.write('\t')
HamLData.write(str(EMGhamL))
HamLData.write('\n')
# ... the rest of your functions ...
#The Program
print "Press ctrl-C to end acquisition"
# a list of your functions
f_list = [
LeftHamAcquisition,
RighHamAcquisition,
LeftVastAcquisition,
RightVastAcquisition,
LeftQuadAcquisition,
RightQuadAcquisition,
]
pool = multiprocessing.Pool() #c reate the worker pool
kill_queue = multiprocessing.Queue() # create the queue to pass poison pills
for f in f_list:
# kick off the functions, passing them the poison pill queue
pool.apply_async(f, args=(kill_queue))
try:
# put the main process to sleep while the workers do their thing
while True:
time.sleep(60)
except KeyboardInterrupt:
# close the workers nicely - put one poison pill on the queue for each
for f in f_list:
q.put(STOP)
pool.close()
pool.join()
raise data.close()
同樣,沒有理由擁有這么多功能。 它們都用不同的字符串和變量名執行相同的操作。 您應該將它們重構為一個函數,可以將參數傳遞給:
def acquisition(kill_queue, f_name, ain):
with open(fname, 'a+') as f:
while True:
try:
val = kill_queue.get(block=False)
if val = STOP:
return
except Queue.Empty:
pass
an_val = ADC.read_raw(ain)
# where does "elapsed_milliseconds" come from? it's undefined
# in your example code
f.write("{}\t{}\n".format(elapsed_milliseconds, an_val))
使用此函數,您無需在我的多處理示例中提供單個函數的列表,而是可以重復使用此函數,並使用不同的參數(這是函數的重點)反復調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.