Skip to main content
 首页 » 编程设计

Python:无法从进程外部设置进程变量

2024年11月01日10mate10pro

我正在尝试从外部设置 Process 类的类变量,以指示进程内的 while 循环应该完成。

然而,尽管变量看起来设置得很好,旧值是从 run(self) 中检索的。我试过在可行的地方使用 threading.Thread。但是,Thread 的问题在于它并不总是在 start() 被调用时启动。有时它会等待 scp 进程完成,这就扼杀了在这种情况下使用线程的意义。

from multiprocessing import Process 
        class LogParserThread(Process): 
            def __init__(self): 
                super(LogParserThread, self).__init__() 
                self.scp_done = False 
                self.flights_to_add = () 
 
            def stop(self): 
                self.scp_done = True 
                print 'Stopping: ' + str(self.scp_done) 
 
 
            def run(self): 
                file_list = () 
                while self.scp_done is False or len(file_list) > 0: 
                    print 'Status ' + str(self.scp_done) + ' ' + str(len(file_list)) 
                    if (len(file_list) > 1): 
                        print ' Case 1 ' 
                        f = file_list[0] 
                        os.system('rm -rf ' + f + '*') 
                    if (len(file_list) is 1 and self.scp_done is True): 
                        print ' Case 2 ' 
                        f = file_list[0] 
                        os.system('rm -rf ' + f + '*') 
                    update file_list 
 
        files_to_copy = "file1 file2 file3" 
        parser_thread = LogParserThread() 
        parser_thread.start() 
        print 'BEFORE COPY ' + str(parser_thread.scp_done) 
        os.system('scp -C remote:' + files_to_copy + ' ' + '/destination/') 
        print 'AFTER COPY ' + str(parser_thread.scp_done) 
        parser_thread.stop() 
        print 'AFTER STOP ' + str(parser_thread.scp_done) 
        parser_thread.join() 
        print 'AFTER JOIN ' + str(parser_thread.scp_done) 

这些是测试运行的打印件:

BEFORE COPY: False 
AFTER COPY False 
Stopping: True 
AFTER STOP True 
Status False 1 
Status False 1 
Status False 1 
Status False 1 
Status False 1 
Status False 1 

请您参考如下方法:

在 Unix 上,子进程是使用 os.fork 生成的。 fork 复制全局变量(或使用 copy-on-write ),因此每个进程(父进程和子进程)都有自己的全局变量副本。在 Windows 上,生成第二个 Python 进程并导入调用模块。 在任何一种情况下(在 Unix 或 Windows 上),当父级修改全局变量时,子级的同名全局变量不会更改。因此在主进程中调用parser_thread.stop()不会影响子进程中self.scp_done的值>.

multiprocessing 确实提供了某些可以促进 sharing state between processes 的对象,例如 mp.Value。对于简单的 bool 值,您还可以使用 mp.Event :

import multiprocessing as mp 
import time 
 
class LogParserProcess(mp.Process): 
    def __init__(self): 
        super(LogParserProcess, self).__init__() 
        self.done = mp.Event() 
        self.flights_to_add = () 
 
    def stop(self): 
        self.done.set() 
        print 'Stopping: ' + str(self.done.is_set()) 
 
    def run(self): 
        file_list = () 
        while not self.done.is_set() or len(file_list) > 0: 
            print 'Status ' + str(self.done.is_set()) + ' ' + str(len(file_list)) 
 
files_to_copy = "file1 file2 file3" 
proc = LogParserProcess() 
proc.start() 
print 'BEFORE COPY ' + str(proc.done.is_set()) 
time.sleep(1) 
print 'AFTER COPY ' + str(proc.done.is_set()) 
proc.stop() 
print 'AFTER STOP ' + str(proc.done.is_set()) 
proc.join() 
print 'AFTER JOIN ' + str(proc.done.is_set()) 

打印

BEFORE COPY False 
Status False 0 
... 
Status False 0 
AFTER COPY False 
Status False 0 
Status False 0 
Status False 0 
Stopping: True 
AFTER STOP True 
AFTER JOIN True