Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

python - Running a Flask server parallel to main app

I have a simple game about hanoi towers that currently works in terminal. It allows the player to input his turn in terminal and outputs a visualization to terminal.

My task is to make a flask app that will open an html page where a JS script would poll the server for info about the game and double the visualization from terminal to the web page.

My problem is that both the game and flask have a main loop and if I run them sequently they won't work parallel.

So I need the game to run in terminal and the player to make turns in terminal, but I need the web server to get the game state and display it.

My question: what should I use for this? Threads of multiprocessing?

Say I have a flask view

from game import game

@app.route('/get_updates')
def get_updates():
   return flask.jsonify(game.instance().board)

How is it gonna work if flask and the game are running in separate threads? How can I get the game object from another thread?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

May be its better to run your game in s different thread?

import threading
import time
from flask import Flask, render_template

class myGame(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.board = 1

    def run(self):
        pass


app = Flask(__name__)
game = myGame()

@app.route('/get_updates') 
def get_updates(): 
    return flask.jsonify(game.board)

if __name__ == "__main__":
    game.start()
    app.run(port=81, host='0.0.0.0', debug=False, use_reloader=False)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...