适用于 Azure 的 Python 3.4 Web 应用程序中的多线程/多处理

Sea*_*sey 8 python multithreading multiprocessing python-3.x azure-web-app-service

我正在使用 Flask 编写一个 Python Web 应用程序,并使用 Azure 进行托管。我需要做一些背景工作。当我在本地测试该应用程序时,一切都很好。但是,一旦我将更新推送到 Azure,它就会停止运行。现在,我已经完成了multithreading.Process设置,并且根据日志文件,Azure 没有启动另一个进程。这是我的代码的相关部分:

#task queue and comm pipes
tasks = Queue()
parent_pipe, child_pipe = Pipe()

def handle_queue_execution(tasks, pipe):
    logging.info("starting task queue handler")
    while True:
        if pipe.recv():
            logging.debug("preparing to get task from queue")
            task = tasks.get()
            args = tasks.get()
            logging.debug("executing task %s(%s)", get_fn_name(task), clean_args(args))
            task(args)
            logging.debug("task %s(%s) executed successfully", get_fn_name(task), clean_args(args))

queue_handler = Process(target=handle_queue_execution, args=(tasks, child_pipe,))
queue_handler.daemon = True

if __name__ == '__main__':
    queue_handler.start()
Run Code Online (Sandbox Code Playgroud)

我对此有一些半相关的问题:

1)为什么Azure不启动另一个进程?

您会注意到,handle_queue_execution 函数以记录器调用开始。当托管在 Azure 上时,该消息不会出现在日志文件中,排队的任务也不会执行。同样,在本地主机上运行时,这两个方面都按预期工作。

2)有更好的方法吗?

我对 Python 和 Azure 都很陌生,因此如果有更好的方法来执行此类任务处理,我很乐意听取。我研究过使用像 Celery 这样的东西,但我不知道如何设置它,而且我更愿意在学习这些新技能时自己实现。

非常感谢。

小智 -1

Python 有多种其他方式来启动新进程。线程在这里很可能是最简单的。

#task queue and comm pipes
import threading
tasks = Queue()
parent_pipe, child_pipe = Pipe()

def handle_queue_execution(tasks, pipe):
    logging.info("starting task queue handler")
    while True:
        if pipe.recv():
            logging.debug("preparing to get task from queue")
            task = tasks.get()
            args = tasks.get()
            logging.debug("executing task %s(%s)", get_fn_name(task), clean_args(args))
            task(args)
            logging.debug("task %s(%s) executed successfully", get_fn_name(task), clean_args(args))

T1 = threading.Thread(target=handle_que_execution, args=(tasks, child_pipe,))

if __name__ == '__main__':
    T1.start()
Run Code Online (Sandbox Code Playgroud)