python3线程机器学习

时间:2020-03-19 07:41:11

标签: python multithreading

有人可以帮我吗,

import threading, time, sys

class Counter:

    def count_x(self, value):
        for _ in range(100000000):
            value += 1
        return value 

    def func_A(self):
        self.some_value = get_the_value_from_the_file()

    def func_B(self):
        print(self.some_value)

    def run(self):
        while True:
            self.func_A()
            self.count_x(self.some_value)
            self.func_B()


app = Counter()
app.run()

得到了上面的代码,我知道这不是很好的做法。 但是我需要使其工作:

first loop:
 func_A - gets value from the file 
 func_count_x = gets value as a parameter and starts doing math on that value
 func_B - prints the value from first loop taken by func_A
2nd loop:
 func_A - gets new value from the file 
 func count_x = is asked if it finished doing math from 1st loop: NO (don't give new value to that function and let it finishing doing math with value from 1st loop)
 func_B - prints value from first loop taken by func_A (beacuse count_x has no answer)

3rd loop:
 func_A - gets new value from the file 
 func count_x = is asked if it finished doing math from 1st loop: YES so now value = answer from that function, count_x gets new value from 3rd loop func_A and it starts doing math on that new value
 func_B - prints newest value from produced by count_x

4th loop:
 func_A - gets new value from the file 
 func count_x = is asked if it finished doing math from 3st loop: NO (don't give new value to that function and let it finishing doing math with value from 3rd loop)
 func_B - prints newest value from produced by count_x

5th loop:
 func_A - gets new value from the file 
 func count_x = is asked if it finished doing math from 3st loop: YES (YES so now value = answer from that function, count_x gets new value from 5rd loop func_A and it starts doing math on that new value)
 func_B - prints newest value from produced by count_x

and go on and go on 

可以肯定的是,在我的程序中还有20-30个函数,例如func A和B,所以我不认为我可以为每个函数创建线程吗? 这是针对ML的,所以我需要确保模型中的答案仅在调用函数且只有在有答案的情况下才覆盖最后的答案

所以我的普通程序正在运行: 1.从相机获取帧 2.在那个框架上做一些事情 3.在该框架上运行我的ML模型并返回答案 4.做其他事情 5.在当前框架上打印模型的答案 我需要它使模型的答案在单独的线程中,以便它可以平稳运行,因此仅当模型有答案时才编写模型的答案,这样程序就不必像示例中那样等待该答案了。制作。

非常感谢大家:)

0 个答案:

没有答案