如何触发APScheduler的flask_websocket发送消息?

时间:2018-09-12 18:22:12

标签: python flask websocket apscheduler flask-sockets

我正在构建一个应用程序以用作待办事项列表。为此,我有了一个Flask后端,该后端存储了一个任务,并使用高级Python计划程序(APScheduler)在截止日期到来时触发了一个事件。

在前端,我有一个Python桌面应用程序,该应用程序通过websockets连接到后端,并等待服务器发送有关期限已到的警报。

具有套接字功能的Flask主服务器

#To add Flask Sockets for double realtime communication.
from flask_sockets import Sockets

#Define a new Flask app.
application = Flask(__name__)
sockets = Sockets(application)

#Import Daemon to schedule tasks.
from Daemon.controller import Daemon
daemon = Daemon()
daemon.startDaemon()

#This is the daemon route that my client connects to.
#This connection remains for as long as the client has the client app open.
@sockets.route('/daemon')
def schedulerTrigger(ws):
  while not ws.closed:
    if daemon.isTask():
      ws.send("Task HERE!")
      print("Task Sent")
    message = ws.receive()
    print(message)
    ws.send(message)
    time.sleep(1)

#After this we got the if__main__ initialization of Flask-sockets.
...

Flask守护程序类,用于使用APScheduler管理预定的活动

#Imports and other stuff

class Daemon():
  def __init__(self):
    super(Daemon, self).__init__()
    self.event = False

  #HERE'S JUST A FUNCTION TO INITIALIZE DAEMON AND USE
  #REMINDPOST TO TELL THE USER THAT THE TASK IS OVERDUE

  def remindPost(self, tskName, tskContent, tskPriority, tskStrtDate, tskEndDate):
    self.event = True
    print("Don't forget to do: ")
    print(tskName)
    print(tskContent)
    print("Priority: " + str(tskPriority))
    print(tskStrtDate)
    print(tskEndDate)

问题是,我在类中有APScheduler,而在主应用程序中有套接字循环。我已经测试过,套接字循环可以单独工作,与APScheduler方法相同。

我希望APScheduler触发套接字循环,以便客户端用户知道任务已过期,但是我无法让它们彼此通信。使 Daemon 类中的APScheduler方法触发套接字循环并向用户发送消息的正确方法是什么?

0 个答案:

没有答案