For a web application in Python running in a web server with WSGI, how to have one single WSGI Worker performing a task?

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

For a web application in Python running in a web server with WSGI, how to have one single WSGI Worker performing a task?

问题

我的Python 3.9和Flask Web应用正在运行在一个使用WSGI的Web服务器上。
随着更多用户连接到Web服务器,WSGI会启动更多的工作进程,但有一些任务必须由单个WSGI工作进程执行,而不是所有工作进程同时执行。

需要由单个工作进程执行的任务包括:

  1. 删除磁盘上的过期文件
  2. 从文件中将一些数据复制到REDIS
  3. 删除各种TXT和LOG文件中的特定行

如果所有工作进程都执行这些任务,就会出现混乱。
如何让一个单独的工作进程执行这些任务,而不是所有工作进程?

英文:

My web application with Python 3.9 and Flask is running in a web server with WSGI.
As more users connect to the web server, more workers are started by WSGI, but there are some tasks that must be performed by one single WSGI worker, rather than all Workers at the same time.

Among such tasks to be performed by one single worker are:

  1. delete obsolete files in the disk
  2. copy some data from a file to REDIS
  3. delete specific lines in various TXT and LOG files

If all workers do such tasks, then a mess starts.
How to have one single worker doing it, rather than all workers?

答案1

得分: 1

你可以考虑实现一个异步任务队列;类似于 celery 这样的工具可以用来完成这项任务,你可以定义任务运行的频率。

英文:

You may want to look into implementing an asynchronous task queue; something like celery would work to do this, you can define the frequency at which tasks run.

huangapple
  • 本文由 发表于 2023年2月19日 05:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496477.html
匿名

发表评论

匿名网友

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

确定