0

每当我尝试此代码和文件更新时,我都会在命令行上收到以下消息:

SystemError:您只能从主可调用对象调用 uwsgi api 函数

任何人都可以向我解释如何正确地做到这一点吗?

谢谢!

#!/usr/bin/env python
import logging
import time
from flask import Flask, render_template
from flask.ext.uwsgi_websocket import WebSocket
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

logging.basicConfig(level=logging.ERROR)

app = Flask(__name__)
ws = WebSocket(app)

class MyEventHandler(FileSystemEventHandler):
    def __init__(self, observer, filename, ws):
        self.observer = observer
        self.filename = filename
        self.ws = ws

    def on_modified(self, event):
        self.ws.send('The file is updated!')

@ws.route('/')
def websocket_server(ws):
    path = '/var/lib/omniwallet/addr/'
    observer = Observer()

    while True:
        address = ws.receive()
        if address is not None:
            filename = address + '.json'

            event_handler = MyEventHandler(observer, filename, ws)
            observer.schedule(event_handler, path, recursive=False)
            observer.start()

            try:
                while True:
                    time.sleep(1)
            except KeyboardInterrupt:
                observer.stop()
            observer.join()

        else: return

if __name__ == '__main__':
    app.run(debug=True, threads=16)
4

1 回答 1

0

您不能使用这种方法,因为观察者在另一个线程中运行,与管理请求的线程完全无关。您可以使用线程队列更改逻辑:MyEventHandler 写入队列,而主要可调用对象等待它并发送回 websocket 消息。

于 2014-06-20T07:51:16.843 回答