0

目前,我正在尝试在 Python 程序中生成一个进程,该程序再次创建线程,不断更新进程地址空间中的变量。到目前为止,我想出了这个运行的代码,但是变量的更新似乎没有传播到进程级别。我本来希望在进程地址空间中定义一个变量并在线程中使用全局(共享进程的地址空间)将允许线程操纵变量并将更改传播到进程。

以下是该问题的一个最小示例:

import multiprocessing 
import threading
import time
import random

def process1():
    lst = {}
    url = "url"
    thrd = threading.Thread(target = urlCaller, args = (url,))
    print("process alive")
    thrd.start()

    while True:
        # the process does some CPU intense calculation
        print(lst)
        time.sleep(2)

def urlCaller(url):
    global lst

    while True:
        # the thread continuously pulls data from an API
        # this is I/O heavy and therefore done by a thread
        lst = {random.randint(1,9), random.randint(20,30)}
        print(lst)
        time.sleep(2)


prcss = multiprocessing.Process(target = process1)
prcss.start()

该进程总是打印一个空列表,而线程按预期打印一个包含两个整数的列表。我希望该过程也会打印一个包含两个整数的列表。(注意:我使用 Spyder 作为 IDE,如果我在 Linux/Ubuntu 上运行此代码,控制台只会打印一些内容,但如果我在 Windows 上的 Spyder 中运行完全相同的代码,控制台上不会打印任何内容。)

我知道使用全局变量并不总是一个好的解决方案,但我认为在这种情况下它可以很好地达到目的。

你可能想知道我为什么要在一个进程中创建一个线程。基本上,我需要对不断变化的不同数据集运行相同的复杂计算。因此,我需要多个进程(每个数据集一个)来优化 CPU 的利用率,并在进程中使用线程来使 I/O 进程最高效。数据贬值速度非常快,因此不能只将其存储在数据库或文件中,这当然会简化数据生产者(线程)和数据消费者(进程)之间的通信过程。

4

2 回答 2

1

您在函数内部定义了一个局部变量 ,所以做什么是无关紧要的,它不能改变不同函数的局部变量。正在定义一个全局变量,但永远看不到它,因为它被您定义的局部变量所遮蔽。lstprocess1urlCallerurlCallerprocess1

您需要lst = {}从该函数中删除并找到另一种方法来返回值或在global那里声明变量:

def process1():
    global lst
    lst = {}
    url = "url"
    thrd = threading.Thread(target = urlCaller, args = (url,))
    print("process alive")
    thrd.start()

    while True:
        # the process does some CPU intense calculation
        print(lst)
        time.sleep(2)

我会直接使用类似的东西concurrent.futures而不是threading模块。

于 2018-12-02T22:45:59.860 回答
0

感谢前面的回答,我发现最好实现一个进程类并在这个类中定义“线程函数”。现在,线程可以访问共享变量并操作该变量,而无需使用“thread.join()”和终止线程。

下面是一个最小示例,其中 2 个并发线程为父进程提供数据。

import multiprocessing
import threading
import time
import random

class process1(multiprocessing.Process):
    lst = {}
    url = "url"

    def __init__(self, url):
        super(process1, self).__init__()
        self.url = url

    def urlCallerInt(self, url):
        while True:
            self.lst = {random.randint(1,9), random.randint(20,30)}
            time.sleep(2)

    def urlCallerABC(self, url):
        while True:
            self.lst = {"Ab", "cD"}
            time.sleep(5)

    def run(self):
        t1 = threading.Thread(target = self.urlCallerInt, args=(self.url,))
        t2 = threading.Thread(target = self.urlCallerABC, args=(self.url,))
        t1.start()
        t2.start()

        while True:
            print(self.lst)
            time.sleep(1)

p1 = process1("url")
p1.start()
于 2018-12-03T21:21:10.103 回答