日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

flask执行python脚本_如何在Flask中运行python脚本

發布時間:2024/2/28 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flask执行python脚本_如何在Flask中运行python脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有一個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()

Flask腳本(注意:價格應為其他腳本的“解析”變量):

# 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)

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的flask执行python脚本_如何在Flask中运行python脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。