同时写入多个文件 [英] Simultaneous write to multiple files

查看:90
本文介绍了同时写入多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,该项目涉及使用Beaglebone读取多个传感器,然后将这些数据传递到文本文件中.我正在监视六种不同的肌肉,因此每个文件都有对应的文件.

I'm working on a project involving using a Beaglebone to read from multiple sensors and then pass that data into a text file.I'm monitoring six different muscles, so I have the files correspond to each.

现在,有6个传感器馈入Beaglebone Black的ADC引脚,我的代码指示Beaglebone为每个引脚创建1个单独的文件.每当我运行以下代码时,我只会得到一个文件(第一个函数运行).以前,我没有包括"while True:"语句,该语句导致1-2个读数和六个文件的创建.我添加了"while True:"来连续记录传感器的数据,因为这是我认为每个文件中的点数不超过2的原因.

Right now, there are 6 sensors feeding into the Beaglebone Black's ADC pins and my code instructs the Beaglebone to create 1 separate file for each pin. Whenever I run the below code, I only get the one file (the first function runs). Previously I had not included the "while True:" statements which resulted in 1-2 readings and six files created. I added the "while True:" to continuously record the sensors' data because that's what I thought was responsible for my not having more than 2 points in each file.

我的问题是:是否可以同时写入多个文件?我也可以将所有这些数据写入同一个文件,但是我想知道是什么使该代码无法正常工作(6个文件).

My question is: is it at all possible to write to multiple files at the same time? I could alternatively write all this data to the same file, but I'm interested in knowing what it is that makes this code not function as intended (6 files.)

        #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创建了一个文件,但是由于它从未返回,因此从未执行过任何其他功能.您需要使用多处理模块之类的东西才能使它们并行运行.特别是,我建议使用多处理池和<一个href ="https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool" rel ="nofollow"> apply_async 函数:

Your function calls have infinite loops in them, so they will never return. One file was created by LeftHamAcquisition, but since it never returned, none of the other functions were ever executed. You need to use something like the multiprocessing module to get them to run in parallel. In particular, I would recommend multiprocessing pools and the apply_async function:

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()

此外,没有理由拥有这么多功能.它们都用不同的字符串和变量名执行相同的操作.您应该将它们重构为一个函数,可以将参数传递给:

Also, there's no reason to have this many functions. They all do the same thing, just with different strings and variable names. You should refactor them into one function that you can pass arguments to:

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))

有了这个函数,您不必在我的多处理示例中提供单个函数的列表,而是可以重复使用此函数,并使用不同的参数(这是函数的重点)反复调用它.

With this function, instead of providing a list of individual functions in my multiprocessing example, you can just reuse this function call it repeatedly with different arguments (which is the whole point of functions).

这篇关于同时写入多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆