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

标签 python multithreading process python-multithreading

我正在尝试从外部设置 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

关于Python:无法从外部进程设置进程变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565511/

相关文章:

python - Beautifulsoup 部分提取字符串

python - 使用 Python 创建文件时保留初始 XML 注释

python - Django 。图片字段。 Django中静态文件的路径

java - 停止运行多个线程的 Java 服务

windows - 如何在 Windows 中监视进程/程序执行?

python - 使用plotly模块时PyCharm显示导入错误?

java - 如何在 Java 中在 Y 线程上运行 X 任务?

java - 线程与 CompletableFuture

c# - 有没有办法用 C# 关闭资源管理器的特定实例?

cocoa - 防崩溃 Mac Cocoa 应用程序