2017-08-11 12 views
0

私はマルチプロセッシングの使い方を学ぶために単純なpythonスクリプトを試作しています。私はバックグラウンドで、ダミーデータを生成し、ユーザーがまだコマンドを入力できる間にキューに入れるプロセスを持っていたいと思います。プロセスはPythonで起動しません

は、ここに私のコードです:

from multiprocessing import Process 
from multiprocessing import Queue 
import random 
import os 

global run 

def waitForStart(q): 
    global run 
    print("(h for help)") 
    while run == 0: 
     response = input("Enter a command: \n") 

     if response == "h": 
      print("h - help\ns - select a profile\no - output a profile\nd - 
download a profile\nd - display selected profile\nr - run selected profile") 

     if response == "r": 
      run = 1 
      beginInterface(q) 
      print("r") 

def generateData(q): 
    data = random.randint(0,100) 
    q.put(data) 

def beginControl(q): 
    print("bcontrol") 
    while run == 1: 
     generateData(q) 
     displayData() 
     adjustOutputs() 
     logData() 
    print("Finished Control") 

def beginInterface(q): 
    global run 
    p.start() 
    print("binterface") 
    while run == 1: 
     response = input("pause? (y/n)\n") 
     print(p.is_alive()) 
     if response == 'y': 
      run = 0 

def displayData(): 
    print(q.get()) 

run = 0 
q = Queue() 
p = Process(target=beginControl , args=(q,)) 
waitForStart(q) 

is_alive呼び出しがp.startは()の数行を前に呼ばれているにもかかわらず、falseを返すので、私は混乱しています。

答えて

0

2つ目のステップのrunは、0という2つのプロセスがあるため、ループに入ることはありません。メインプロセスと第2プロセスの間で通信するには、Pipe or a Queueを使用します。

重要な注意事項、追加することを忘れないでください勝利の場合:

def displayData(q): 
    print(q.get()) 

if __name__ == "__main__": 

See reason here.

もう一つは、あなたが編集し、displayDataにキューを渡すことはありません、あなたが本当に望んでいるのは、スレッドです。このコードはプロセスを無限ループに入れます。

これはまさにあなたのコードではありませんが、それはアイデア伝え:

from multiprocessing import Process 
from multiprocessing import Queue, Pipe 
import random 
import time 


def waitForStart(q): 
    global run 
    print("(h for help)") 
    while True: 
     response = input("Enter a command: \n") 

     if response == "h": 
      print("h - help" 
        "s - select a profile" 
        "o - output a profile" 
        "d - download a profile" 
        "d - display selected profile" 
        "r - run selected profile") 

     if response == "r": 
      run = 1 
      beginInterface() 
      print("r") 


def generateData(q): 
    data = random.randint(0,100) 
    q.put(data) 


def beginControl(q): 
    print("bcontrol") 
    while q.empty(): 
     generateData(q) 
     displayData(q) 
     #adjustOutputs() 
     #logData() 
    print("Finished Control") 


def beginInterface(): 
    p.start() 
    print("binterface") 
    while True: 
     print(p.is_alive()) 
     time.sleep(1) 
     q.put('DIE!') 
     p.join() 
     exit() 


def displayData(q): 
    data = q.get() 
    if data != 'DIE!': 
     print(data) 


if __name__ == "__main__": 
    q = Queue() 
    pip = Pipe() 
    p = Process(target=beginControl, args=(q,)) 
    waitForStart(q) 
+0

感謝を!私はパイプ上で少しぼやけているので、そのトピックを読んでみましょう。私は、プロセスをユーザー入力によって制御されたループで実行させようとしています。私がしたいのは、ユーザーがまだコマンドを入力できる間に、プロセスが継続的に画面にデータを生成し、印刷することです。最終的に、バックグラウンドプロセスがセンサからのデータを記録するLabViewに接続されたコードにこれを拡大したいと思っています。ユーザーはフロントエンドでプログラムを制御できる必要があります。 – egemnay

+0

@egemnay、上記のように、必要なのはプロセスではなくスレッドです - https://docs.python.org/2/library/threading.html?highlight=threading#module-threading – droravr

関連する問題