英文:
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工作进程执行,而不是所有工作进程同时执行。
需要由单个工作进程执行的任务包括:
- 删除磁盘上的过期文件
- 从文件中将一些数据复制到REDIS
- 删除各种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:
- delete obsolete files in the disk
- copy some data from a file to REDIS
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论