如何在python-flask框架中运行永久后台进程?

huangapple go评论59阅读模式
英文:

How to run a permanent background process within the python-flask framework?

问题

I am about to code a simple browser-based game in which players can connect to it via browser using python and flask. Now, I want this game still to be running when no player is connected to the backend, as things are going on in the game. That can be handled by a separate process running and accessing a database, and updating entries on the database when some event happens.

Is there then a simple way to incorporate that into a flask appication I am currently running as

import sys
import json
from flask import Flask, request, render_template

app = Flask(name)

from mf import gameplay

game = gameplay.Game()

@app.route('/')
def index():
return render_template('mainmap.html')

@app.route('/api', methods=['POST'])
def api():
data_json = request.data.decode()
data = json.loads(data_json)
return game(data_json)

if name == 'main':
app.run('0.0.0.0', 5000, threaded=True)

And is there a way to "notify" the background process when a player is (re)connecting to the game, so that the background process "knows" a new player has connected, and the background process performs some specific actions?

英文:

I am about to code a simple browser-based game in which players can connect to it via browser using python and flask. Now, I want this game still to be running when no player is connected to the backend, as things are going on in the game. That can be handled by a separate process running and accessing a database, and updating entries on the database when some event happens.

Is there then a simple way to incorporate that into a flask appication I am currently running as

import sys
import json
from flask import Flask, request, render_template

app = Flask(__name__)

from mf import gameplay

game = gameplay.Game()

@app.route('/')
def index():
	return render_template('mainmap.html')

@app.route('/api', methods=['POST'])
def api():
	data_json = request.data.decode()
	data = json.loads(data_json)
	return game(data_json)

if __name__ == '__main__':
	app.run('0.0.0.0', 5000, threaded=True)

And is there a way to "notify" the background process when a player is (re)connecting to the game, so that the background process "knows" a new player has connected, and the background process performs some specific actions?

答案1

得分: 1

你可以使用任务队列或消息代理系统来处理异步处理。

示例包括:Dramatiq、Huey、Redis Queue、APScheduler、QueueWorker、Celery等等。

一个示例是Celery

pip install celery

你可以创建一个文件来包含你的后台任务,我们称之为background_tasks.py

from celery import Celery

创建一个Celery实例。这可以做得更好。我写了一个快速的代码

celery = Celery('background_tasks', broker='pyamqp://guest@localhost//')

@celery.task
def process_game_event(event_data):
# 在这里你可以应用你的游戏事件,执行特定的操作等等

然后在你的app.py中,你可以整合后台进程:

...
from background_tasks import process_game_event
...

@app.route('/api', methods=['POST'])
def api():
data_json = request.data.decode()
data = json.loads(data_json)
process_game_event.delay(data_json) # 使用Celery异步处理事件
return game(data_json)

再次强调,你可以使用其他替代方案。这只是众多示例之一,根据你的用例,你的逻辑会有所不同。

英文:

You can use a task queue or a message broker system to handle asynchronous processing.

Examples include: Dramatiq, Huey, Redis Queue, APScheduler, QueueWorker, Celery, and so on.

An example out of many is Celery

pip install celery

You can create a file that has your background tasks; let us call it background_tasks.py

from celery import Celery

# Create a Celery instance. This can be done better. I am writing quick code
celery = Celery('background_tasks', broker='pyamqp://guest@localhost//')

@celery.task
def process_game_event(event_data):
    # Here you can apply your game events, perform specific actions, etc

Then in your app.py, you can incorporate the background process:

...
from background_tasks import process_game_event
...

@app.route('/api', methods=['POST'])
def api():
    data_json = request.data.decode()
    data = json.loads(data_json)
    process_game_event.delay(data_json) # Process events asynchronously using Celery
    return game(data_json)

Once again, yiou can use any other alternatives. This is only an example out of many, and your logic will be different based on your usecases.

huangapple
  • 本文由 发表于 2023年5月7日 16:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定