注销后如何进行Django后台任务

时间:2019-03-03 12:38:03

标签: django background-process

我想在Django中创建一个后台进程,该进程在按下开始按钮时会循环某些功能,并在按下停止按钮时会停止循环。当用户“脱机”(注销)时,它必须工作。每个用户都应有单独的过程。

到目前为止,我已经找到了使用Threading的方法,但是我还不知道它如何工作,我刚刚创建了一个开始或停止Threads的类,它看起来像这样:

class Decide(threading.Thread):
    """Thread for making decision to buy or sell """
    def __init__(self, name):
        threading.Thread.__init__(self)
        # flag to pause thread
        self.paused = True
        self.pause_cond = threading.Condition(threading.Lock())
        self.pause_cond.acquire()
        self.name = name

    def run(self):
        while True:
            with self.pause_cond:
                while self.paused:
                    self.pause_cond.wait()

                # Buy_wait_sell()
                print("I am looping~!!")
            time.sleep(1)

    def pause(self):
        # Acquire to lock worker
        self.paused = True
        self.pause_cond.acquire()

    # should just resume the thread
    def resume(self):
        self.paused = False
        # Notify so thread will wake after lock released
        self.pause_cond.notify()
        # Now release the lock
        self.pause_cond.release()

这是一个好概念吗?我如何为用户分配线程?也许models方法?当我注销时,该命令实际上是否正在运行,否则django将终止该过程?在创建用户时启动Thread时,效率会低吗?

0 个答案:

没有答案