python - 如何在Python的main函数中处理从线程函数返回的数据

标签 python multithreading

我有一个 python 脚本,我在其中创建了一个线程。下面是代码片段

stop_thread = False
get_data = False
status = ""
subject_id = ""


def get_data_function():
    global stop_thread
    global get_data 
    global status
    global subject_id

    while stop_thread is False:
        if get_data:
            # SOME CODE
            # SOME MORE CODE
            (status, subject_id) = tive.extract_data()

            get_data = False

        time.sleep(1)


def main():
    global stop_thread
    global get_data 
    global status
    global subject_id

    thread = Thread(target=get_data_function)
    thread.start()
    res_dict = dict()
    while True:
        # SOME CODE
        # SOME MORE CODE

        if some_condition:
            get_data = True

            res_dict['status'] = status
            res_dict['subject_id'] = subject_id

        # SOME CODE
        # SOME MORE CODE

在上面的代码中,我定义了一个线程及其函数get_data_function()。该函数调用tive.extract_data(),它给出status, subject_id。我已将这些变量定义为全局变量,以便一旦我们获得这些变量的值,我就可以在 main 函数中使用它。

main函数中,some_conditionTrue后,我们需要获取status的值>subject_id 所以我将全局变量 get_data 设置为 True 这启用了 get_data_function 主代码并返回数据,但问题是tive.extract_data() 需要 2-3 秒才能响应,因为 res_dict['status'] = statusres_dict['subject_id'] = subject_id 在 main 函数中给出了 2-3 秒的错误,之后它开始正常工作。

是否有任何其他方法以优化的方式处理这些变量的值,以便在我们没有这些变量的值之前,我们不会收到错误。请帮忙。谢谢

最佳答案

我会定义一个线程类。我使用全局变量的经历很糟糕。

# ==================================================================================
class ThreadGetData(Thread):

    def __init__(self):
        super().__init__()

        self.stop_thread = False
        self.get_data = False
        self.status = ""
        self.subject_id = ""
        self.data_present = False

    # ------------------------------------------------------------------------------
    def get_data_function(self):

        if self.get_data:
            # SOME CODE
            # SOME MORE CODE
            (self.status, self.subject_id) = tive.extract_data()
            self.data_present = True
            self.get_data = False

        time.sleep(1)

    # ------------------------------------------------------------------------------
    def run(self):
        while not self.stop_thread:
            self.get_data_function()



# ==================================================================================
def main():

    thread = ThreadGetData()
    thread.start()

    res_dict = dict()
    while True:
        # SOME CODE
        # SOME MORE CODE

        if some_condition:
            thread.get_data = True

       if thread.data_present:
            res_dict['status'] = thread.status
            res_dict['subject_id'] = thread.subject_id
            self.data_present = False

        # SOME CODE
        # SOME MORE CODE

关于python - 如何在Python的main函数中处理从线程函数返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60950397/

相关文章:

python - Tensorflow - 如何阅读预测

python - 从同一类定义中实例化 python 类

c# - C# 和 .net 中的 ExitThread(ExitCode) 和 GetExitCodeThread 等价物是什么?

python - 使用线程在 ZeroMQ REQ/REP 模式的服务器端创建许多回复器套接字

android - 在 android 中进行 http 调用的最佳做法是什么

python 3 : does Pool keep the original order of data passed to map?

python - 动态过度绘制

python - statsmodels 线性回归 - patsy 公式以包含模型中的所有预测变量

python - 如何避免Django子线程被uWSGI respawn杀死

python - Cassandra 或 MongoDB 可实现良好的扩展性和大量查询