我有一个Flask
脚本,可以创建一个网站并动态打印一些数据。 -打印的数据应该来自另一个python脚本。
我目前面临的问题是,如果我将执行python脚本的行放在执行Flask
应用的行之前,它将运行Python脚本而不运行Flask
;反之亦然。
Python脚本:
import websocket
from bitmex_websocket import Instrument
from bitmex_websocket.constants import InstrumentChannels
from bitmex_websocket.constants import Channels
import json
websocket.enableTrace(True)
sells = 0
buys = 0
channels = [
InstrumentChannels.trade,
]
XBTUSD = Instrument(symbol='XBTUSD',
channels=channels)
XBTUSD.on('action', lambda msg: test(msg))
def test(msg):
parsed = json.loads(json.dumps(msg))
print(parsed)
XBTUSD.run_forever()
烧瓶脚本(注意:价格应为其他脚本的“解析”变量):
# Start with a basic flask app webpage.
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event
import requests, json
import time
__author__ = 'slynn'
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
#turn the flask app into a socketio app
socketio = SocketIO(app)
#random number Generator Thread
thread = Thread()
thread_stop_event = Event()
class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()
def randomNumberGenerator(self):
while not thread_stop_event.isSet():
socketio.emit('newnumber', {'number': parsed}, namespace='/test')
sleep(self.delay)
def run(self):
self.randomNumberGenerator()
@app.route('/')
def index():
#only by sending this page first will the client be connected to the socketio instance
return render_template('index.html')
@socketio.on('connect', namespace='/test')
def test_connect():
# need visibility of the global thread object
global thread
print('Client connected')
#Start the random number generator thread only if the thread has not been started before.
if not thread.isAlive():
print("Starting Thread")
thread = RandomThread()
thread.start()
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app)
答案 0 :(得分:4)
使用internals_type
:
using System;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Concurrent;
namespace Test
{
class Program
{
static void Main(string[] args)
{
GetFilesOnRoot("*");
Console.ReadLine();
}
private static ConcurrentBag<string> FilesBag;
private static void GetFilesOnRoot(string filter)
{
FilesBag = new ConcurrentBag<string>();
DirectoryInfo dirRoot = new DirectoryInfo("/");
GetDirTree(dirRoot, "*");
}
private static void GetDirTree(DirectoryInfo dr, string filter)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
try
{
files = dr.GetFiles(filter + ".*");
}
catch(Exception) { }
if (files != null)
{
Parallel.ForEach(files, (FileInfo item) => { FilesBag.Add(item.Name); });
subDirs = dr.GetDirectories();
Parallel.ForEach(subDirs, (DirectoryInfo item) => { GetDirTree(item,filter); });
}
}
public static async Task GetFilesOnRootAsync(string filter)
{
await Task.Run(() => {
GetFilesOnRoot(filter);
});
}
}
}
)生成的内容包装到函数中。import
或website_generator.py
相同的目录中。app.py
中使用flask.py
from website_generator import function_name
您可以使用其他功能,例如flask.py
等;尽管他们可能不会给您答复。
使用function_name()
的示例:
subprocess.call
答案 1 :(得分:0)
尝试一下:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def run_script():
file = open(r'/path/to/your/file.py', 'r').read()
return exec(file)
if __name__ == "__main__":
app.run(debug=True)