具有2个主/进程的Python应用程序结构

时间:2016-10-30 00:52:22

标签: python structure popen python-3.5

这可能是一个非常简单的答案,但我是新手创建自己的python应用程序和包。我非常熟悉C ++和C#等语言及其库。

我正在尝试构建一个python包,除了像简单的可执行文件一样运行。我有这个应用程序的支持模块,它们只是实例化和调用的类。但是我有一个问题。我有两个主要功能。我背后的原因是我有一个程序的一部分,它包含简单的for循环,它实例化其中一个类,然后给出关于这个类的特定细节(输入进入它等)我用调用'python'创建一个Popen实例smartinterface.py'和smartinterface.py包含另一个主要功能。我想知道如何才能最好地构建这个。我应该创建两个不同的包,并弄清楚如何链接包含类的模块,因为两个进程都依赖于相同的类,或者我对Popen做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试类似的东西。

import asyncio, random


loop = asyncio.get_event_loop()

@asyncio.coroutine
def second_task():
    while True:
        yield from asyncio.sleep(0.5 + random.random())

@asyncio.coroutine
def main_task():
    while True:
        # do somthing
        loop.create_task(consume())


loop.create_task(main_task())
loop.run_forever()

使用任务队列的另一个例子(我认为它更好):

import asyncio, random

task_queue = asyncio.Queue()

@asyncio.coroutine
def task_producer():
    while True:
        yield from task_queue.put(random.random())
        yield from asyncio.sleep(random.random())

@asyncio.coroutine
def task_consumer():
    while True:
        value = yield from task_queue.get()
        print("returne value: ", value)


loop = asyncio.get_event_loop()
loop.create_task(task_producer())
loop.create_task(task_consumer())
loop.run_forever()