线程,多处理共享内存和asyncio

时间:2018-04-06 12:16:38

标签: python multithreading python-3.x multiprocessing python-asyncio

我无法实施以下方案:

class A:
    def __init__(self):
        self.content = []
        self.current_len = 0

    def __len__(self):
        return self.current_len

    def update(self, new_content):
        self.content.append(new_content)
        self.current_len += 1

class B:
    def __init__(self, id):
        self.id = id

我还有这两个函数,稍后将在main中调用:

async def do_stuff(first_var, second_var):
    """ this function is ideally called from the main in another 
        process. Also, first_var and second_var are not modified so it 
        would be nice if they could be given by reference without 
        having to copy them """

    ### used for first call
    yield None
    while len(first_var) < CERTAIN_NUMBER:
        time.sleep(10)

    while True:
        ## do stuff
        if condition_met:
            yield new_second_var ## which is a new instance of B
        ## continue doing stuff

def do_other_stuff(first_var, second_var):
    while True:
        queue = multiprocessing.JoinableQueue()
        results = multiprocessing.Queue()
        ### do stuff
        first_var.update(results)

此时主要看起来像这样:

first_var = A()
second_var = B()
while True:
    async for new_second_var in do_stuff(first_var, second_var):
        if new_second_var:
            ## stop the do_other_stuff that is currently running
            ## to re-launch it with the updated new_var
            do_other_stuff(first_var, new_second_var)
        else: ## used for the first call
            do_other_stuff(first_var, second_var)

以下是我的问题:

  • 有没有更好的解决方案让这个方案有效?
  • 如何实现“停止”部分,因为有一段时间的True循环通过引用填充first_var?
  • 如果在其中没有修改first_var,是否会通过引用do_stuff传递A(first_var)的实例?
  • 甚至可以在另一个进程中使用异步生成器吗?
  • 甚至可能吗?

这是使用Python 3.6作为异步生成器。

我希望这有点清楚!非常感谢!

0 个答案:

没有答案