我有一个工作线程,唯一的任务是每10分钟从数据库中查询活动用户列表,并在满足一定条件时(每分钟检查一次)向他们发送一条SMS消息;而且工作线程根本不会阻碍主应用程序。
到目前为止,我设法使线程正常运行并发送SMS也可以。但是,由于某些原因,线程会在某个随机时间(小时)后停止/被杀死。我运行try:除了e:Exception之外,在一段时间内为True,以捕获发生的错误。另外,我打印出一条消息,指出发生了什么错误。
好吧,我再也没有看到任何消息,线程肯定已关闭。因此,我怀疑Gunicorn或Django会优雅地杀死我的线程。
我已经在整个代码中放置了日志和打印语句,但是没有发现任何指示我的线程被杀死的原因。
我的wsgi.py函数在其中调用该函数以启动线程
"""
WSGI config for django_web project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_web.settings')
application = get_wsgi_application()
'''
Start background services
Import has to happen after "get_wsgi_application()"; otherwise docker container crashes
'''
try:
from threading import Thread
from timesheet.admin import runWorkmateServices
runWorkmateServices()
except Exception as exp:
print(exp)
从wsgi.py内部调用的函数。我仔细检查了线程是否已启动,以避免两个线程同时启动并运行。
def runWorkmateServices(request=None):
service_name = 'TimeKeeperWorkMateReminderService'
thread_found = False
for thread in threading.enumerate():
if service_name in thread.name:
thread_found = True
break # Leave loop now
if thread_found:
print(f'Service has already been started: {service_name}')
if request:
messages.add_message(request, messages.ERROR, f'Service has already been started:: {service_name}')
else:
Thread(target=WorkMateReminders, args=(), name=service_name, daemon=True).start()
print(f'Started Service: {service_name}')
if request:
messages.add_message(request, messages.SUCCESS, f'Started Service: {service_name}')
工作线程本身
def WorkMateReminders():
print('Thread Started: WorkMateReminders')
timer = 0
employees = User.objects.none()
while True:
try:
# Update user list every n * sleep time (10 minutes)
if timer % 10 == 0:
timer = 0
# Get active employees
employees = User.objects.filter(is_active=True, profile__workmate_sms_reminders_activated=True)
print(f'Employees updated at {datetime.now().date()} - {datetime.now().time()}: {employees}')
WorkMateCheckClockOffTimes(employees=employees)
WorkMateClockOnReminder(employees=employees)
WorkMateEndOfBreakReminder(employees=employees)
timer += 1 # increment timer
except Exception as exp:
print(f'Error: {exp}')
time.sleep(60 * 1)
我的目标是让此工作线程在Django启动期间一直运行。
答案 0 :(得分:1)
大多数WSGI服务器都会定期生成/杀死工作线程,而从这些工作线程中生成线程并不是解决问题的最佳方法。有几种解决方法
Cron
Create a management command执行所需的操作,并将cron配置为每10分钟运行一次
芹菜/芹菜节
设置一个celery工作者,这是一个与Django应用程序异步运行的过程,使用celerybeat您可以使任务间隔运行