尝试运行python文件updater.py
以SSH方式连接到服务器,并每隔几段时间运行一些命令。我正在使用APScheduler从__init__.py
运行函数update_printer()
。最初我得到了一个working outside of application context error
,但是有人建议我从__init__
.py导入应用程序。不过,结果不太好。我一直收到一个cannot import name 'app'
错误。
应用程序py
from queue_app import app
if __name__ == '__main__':
app.run(debug=True)
初始化py
from flask import Flask, render_template
from apscheduler.schedulers.background import BackgroundScheduler
from queue_app.updater import update_printer
app = Flask(__name__)
app.config.from_object('config')
@app.before_first_request
def init():
sched = BackgroundScheduler()
sched.start()
sched.add_job(update_printer, 'interval', seconds=10)
@app.route('/')
def index():
return render_template('index.html')
更新程序.py
import paramiko
import json
from queue_app import app
def update_printer():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(app.config['SSH_SERVER'], username = app.config['SSH_USERNAME'], password = app.config['SSH_PASSWORD'])
...
文件结构
queue/
app.py
config.py
queue_app/
__init__.py
updater.py
错误
Traceback (most recent call last):
File "app.py", line 1, in <module>
from queue_app import app
File "/Users/name/queue/queue_app/__init__.py", line 3, in <module>
from queue_app.updater import update_printer
File "/Users/name/queue/queue_app/updater.py", line 3, in <module>
from queue_app import app
ImportError: cannot import name 'app'
要从updater.py访问app.config并避免从APScheduler运行时出现“在应用程序上下文之外工作”错误,我需要做什么?
这是一个循环依赖项,当您在
__init__.py
文件中导入updater
时。在我的烧瓶设置中,app
是在app.py
中创建的。相关问题 更多 >
编程相关推荐