簡體   English   中英

是否可以在Python的線程內生成進程?

[英]Is it possible to spawn a process inside a thread in Python?

我正在編寫一個程序,該程序生成一個進程並在某些條件下重新啟動該進程。 例如,如果子進程在某個時間段內不再向母進程發送數據,我希望母進程終止子進程並重新啟動它。 我以為可以使用線程從子進程中接收數據並重新啟動子進程,但是它無法按照我的方式工作。

import numpy as np
import multiprocessing as mp
import threading
import time
from apscheduler.schedulers.background import BackgroundScheduler

pipe_in, pipe_out = mp.Pipe()

class Mother():
    def __init__(self):
        self.pipe_out = pipe_out

        self.proc = mp.Process(target = self.test_func, args=(pipe_in, ))
        self.proc.start()

        self.thread = threading.Thread(target=self.thread_reciever, args=(self.pipe_out, ))
        self.thread.start()

    def thread_reciever(self, pipe_out):
        while True:
            value = pipe_out.recv()

            print(value)
            if value == 5:
                self.proc.terminate()
                time.sleep(2)
                self.proc = mp.Process(target = self.test_func)
                self.proc.start()

    def test_func(self, pipe_in):
        for i in range(10):
            pipe_in.send(i)
            time.sleep(1)


if __name__ == '__main__':
    r = Mother()

它打印出此錯誤。

D:\>d:\python36-32\python.exe temp06.py
    0
1
2
3
4
5
Exception in thread Thread-1:
Traceback (most recent call last):
File "d:\python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
File "d:\python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
File "temp06.py", line 28, in thread_reciever
    self.proc.start()
File "d:\python36-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
File "d:\python36-32\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
File "d:\python36-32\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
File "d:\python36-32\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
File "d:\python36-32\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects


D:\>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "d:\python36-32\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "d:\python36-32\lib\multiprocessing\reduction.py", line 82, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87]

如何啟動和終止線程內的進程? (我正在使用線程,因為它可以同步接收來自其他進程的數據)還是有其他方法可以完成此工作?

test_func作為全局函數

import numpy as np
import multiprocessing as mp
import threading
import time
from apscheduler.schedulers.background import BackgroundScheduler

pipe_in, pipe_out = mp.Pipe()  

def test_func( pipe_in):
    for i in range(10):
        pipe_in.send(i)
        time.sleep(1)

class Mother():
    def __init__(self):
        self.pipe_out = pipe_out
        mp.freeze_support()
        self.proc = mp.Process(target = test_func, args=(pipe_in, ))
        self.proc.start()

        self.thread = threading.Thread(target=self.thread_reciever, args=(self.pipe_out, ))
        self.thread.start()

    def thread_reciever(self, pipe_out):
        while True:
            value = pipe_out.recv()

            print(value)
            if value == 5:
                self.proc.terminate()
                time.sleep(2)
                mp.freeze_support()
                self.proc = mp.Process(target = test_func, args=(pipe_in,))
                self.proc.start()


if __name__ == '__main__':

    r = Mother()

輸出值

D:\> d:\python36-32\python.exe temp06.py
0
1
2
3
4
5
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "d:\python36-32\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
File "d:\python36-32\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'test_func' on <module '__main__' (built-in)>

在Windows下,由於沒有fork系統調用,因此python啟動一個新的解釋器實例,使用pickle / unpickle重構執行上下文,但是thread.Lock不可thread.Lock 腌制self.test_func ,將self.thread引用到thread.Lock對象,使其無法腌制。

您可以簡單地將test_func更改為簡單的全局函數,而無需引用線程對象:

self.proc = mp.Process(target = test_func, args=(pipe_in,))
...
def test_func(pipe_in):
    for i in range(10):
        pipe_in.send(i)
        time.sleep(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM