2016-04-24 15 views
0

私は6つのセンサーを使って6つの異なるファイルにデータを保存するプロジェクトを開発中です。別のSOの質問(https://stackoverflow.com/a/36634587/2615940)を介して、私はマルチプロセッシングモジュールが私のためにこれを行うことを学んだが、私の新しいコードを実行すると、6つではなく1つのファイルしか得られない。 ?Pythonマルチプロセッシングを使ったファイルの同時書き込み

*下記のskrrgwasmeの提案に基づいてManagerを含むようにファイルを編集しましたが、コードが実行されて何も生成されません。エラーもファイルもありません。ちょうど走る。

コード:

import Queue 
import multiprocessing 
import time 

def emgacq(kill_queue, f_name, adcpin): 
    with open(f_name, '+') as f: 
     while True: 
      try: 
       val = kill_queue.get(block = False) 
       if val == STOP: 
        return 
      except Queue.Empty: 
       pass 
      an_val = ADC.read(adcpin) * 1.8 
      f.write("{}\t{}\n".format(ms, an_val)) 
def main():  
    #Timing stuff 
    start = time.time() 
    elapsed_seconds = time.time() - start 
    ms = elapsed_seconds * 1000 

    #Multiprcessing settings 
    pool = multiprocessing.Pool() 
    m = multiprocessing.Manager() 
    kill_queue = m.Queue()    

    #All the arguments we need run thru emgacq() 
    arg_list = [ 
     (kill_queue, 'HamLeft', 'AIN1'), 
     (kill_queue, 'HamRight', 'AIN2'), 
     (kill_queue, 'QuadLeft', 'AIN3'), 
     (kill_queue, 'QuadRight', 'AIN4'), 
     (kill_queue, 'GastLeft', 'AIN5'), 
     (kill_queue, 'GastRight', 'AIN6'), 
     ] 

    for a in arg_list: 
     pool.apply_async(emgacq, args=a) 

    try: 
     while True: 
      time.sleep(60) 
    except KeyboardInterrupt: 
     for a in arg_list: 
      kill_queue.put(STOP) 
     pool.close() 
     pool.join() 
     raise f.close() 

if __name__ == "__main__": 
    main() 
+0

は、あなたがこれらの質問の両方のために対処してきた問題を考えると、私は強くあなたには、いくつかの基本的なPythonのチュートリアルを通過示唆しています。関数呼び出し、変数の代入、引数の受け渡しなどの基本的な考え方については混乱があるようです。あなたは、あなたが次のスクリプト/プログラムに飛び込む前に、それらの基礎を理解することができれば、より多くの成功を収めます。 – skrrgwasme

答えて

2

あなたの主な問題は、あなたのサブプロセス関数の引数のリストが間違っているということです。

f_list = [ 
    emgacq(kill_queue, 'HamLeft', 'AIN1'), 
    # this calls the emgacq function right here - blocking the rest of your 
    # script's execution 

はまた、あなたのapply_async呼び出しが間違っている:

for f in f_list: 
    pool.apply_async(f, args=(kill_queue)) 
    # f is not a function here - the arguments to the apply_async function 
    # should be the one function you want to call followed by a tuple of 
    # arguments that should be provided to it 

これはまた、managerを含むEUE(https://stackoverflow.com/a/9928191/2615940を参照)、main機能であなたのすべてのコードを置く:

# put your imports here 
# followed by the definition of the emgacq function 

def main(): 

    #Timing stuff 
    start = time.time() 
    elapsed_seconds = time.time() - start 
    ms = elapsed_seconds * 1000 

    pool = multiprocessing.Pool() 
    m = multiprocessing.Manager() 
    kill_queue = m.Queue() 

    arg_list = [ 
      (kill_queue, 'HamLeft', 'AIN1'), 
      (kill_queue, 'HamRight', 'AIN2'), 
      (kill_queue, 'QuadLeft', 'AIN3'), 
      (kill_queue, 'QuadRight', 'AIN4'), 
      (kill_queue, 'GastLeft', 'AIN5'), 
      (kill_queue, 'GastRight', 'AIN6'), 
     ] 

    for a in arg_list: 
     pool.apply_async(emgacq, args=a) 
     # this will call the emgacq function with the arguments provided in "a" 

if __name__ == "__main__": 
    # you want to have all of your code in a function, because the workers 
    # will start by importing the main module they are executing from, 
    # and you don't want them to execute that code all over again 
    main() 
+0

ここで何が起こっているのか分かりません。今、継承の問題があるようです。具体的には、「キューオブジェクトは継承を介してプロセス間でのみ共有する必要があります。 – boktor

+0

@boktor編集を参照してください。 – skrrgwasme

+0

メインポストを編集して、 'Manager 'を試したときに何が起きたのかを確認できます。定義が変更されたので、' kill_queue'をどこで使うか調整する必要がありますか? ( 'kill_queue = m.Queue()'ではなく 'kill_queue = multiprocessing.Queue'です) – boktor

関連する問題