0

Python multiprocessing を使用して関数を記述しようとしています。関数を制御して「コマンド」を渡してプロセスを完全に終了させることができます。私はいくつかの例を探して試してみましたが、うまくいかなかったようです.
だから基本的には、whileループアクションを実行する別のプロセス関数コードを実行する必要があり、必要に応じてコマンドを渡して終了する必要があります。アドバイスをお願いします

例 1

from multiprocessing import Process, Queue

def start_process(queue):
  while True:
    try:
        m = queue.get()
        if m == 'exit':
            print ('cleaning up worker...')
            # add here your cleaning up code
            break
        else:
            print (m)
    except KeyboardInterrupt:
        print ('ignore CTRL-C from worker')


if __name__ == '__main__':
  queue = Queue()

  process = Process(target=start_process, args=(queue,))
  process.start()

  queue.put(12)

  try:
    process.join()
  except KeyboardInterrupt:
    print ('wait for worker to cleanup...')
    queue.put('exit')
    process.join()

例 2

import multiprocessing
    import time

    class MyProcess(multiprocessing.Process):

        def __init__(self, ):
            multiprocessing.Process.__init__(self)
            self.exit = multiprocessing.Event()

        def run(self):
            while not self.exit.is_set():
                pass
            print ("You exited!")

        def shutdown(self):
            print ("Shutdown initiated")
            self.exit.set()


    if __name__ == "__main__":
        process = MyProcess()
        process.start()
        print ("Waiting for a while")
        time.sleep(3)
        process.shutdown()
        time.sleep(3)
        print ("Child process state: %d" % process.is_alive())
4

1 に答える 1

0

どちらの例も私にとっては問題なく機能します。おそらく、それらがどのように機能するかを誤解していますか?

最初の例では、メイン スレッドが実行されると、子スレッドが開始され、12 が送信されます。その後、子スレッドに参加するのを待ちます。その時点で、子は「終了」を待っているため、すべてが停止します。しかし、その後 ctrl-C を押すと、'exit' が送信され、子プロセスが終了し、2 番目の参加が成功します:

> python3.3 example1.py                                                    
12                                                                              
^Cignore CTRL-C from worker                                                     
wait for worker to cleanup...                                                   
cleaning up worker...                                                           
>

親に「exit」を送信してからすべてを終了させたい場合は、次を使用します。

def start_process(queue):                                                       
  while True:                                                                   
    try:                                                                        
        m = queue.get()                                                         
        if m == 'exit':                                                         
            print ('cleaning up worker...')                                     
            # add here your cleaning up code
            break
        else:
            print (m)
    except KeyboardInterrupt:
        print ('ignore CTRL-C from worker')
  print('goodbye cruel world')


if __name__ == '__main__':
  queue = Queue()

  process = Process(target=start_process, args=(queue,))
  process.start()

  queue.put(12)
  print ('sending exit')
  queue.put('exit')
  process.join()

与える:

> python3.3 my-example.py 
sending exit
12
cleaning up worker...
goodbye cruel world
> 

2番目の例も機能します(インデントが修正されています):

> python3.3 example2.py 
Waiting for a while
Shutdown initiated
You exited!
Child process state: 0
>

(ちょっと待ってください)。ここで他に何が期待できるかわかりません。

于 2013-08-10T20:24:21.027 に答える