Код: Выделить всё
@app.task(base=AbortableServerSideContextTask)
def test_watch_dog():
from common.utils.test_with_heartbeat import TestWithWatchDog
with TestWithWatchDog():
times = 0
while times < 10:
from flask import current_app
current_app.logger.info(f'main thread counting times:{times}')
times += 1
time.sleep(2) # processing
Код: Выделить всё
import threading
from flask import current_app
class TestWithWatchDog:
def __init__(self):
self.watch_dog = threading.Thread(target=self._watch_dog, daemon=False)
self.watch_dog_shutdown_event = threading.Event()
self.logger = current_app.logger
def __enter__(self):
self.logger.info("TestWithHeartBeat entered")
self.watch_dog.start()
def __exit__(self, exc_type, exc_val, exc_tb):
self.watch_dog_shutdown_event.set()
self.watch_dog.join()
current_app.logger.info("TestWithHeartBeat exited")
def _watch_dog(self):
self.logger.info('watch_dog started')
while not self.watch_dog_shutdown_event.is_set():
self.logger.info(f'watch_dog heartbeat!')
self.watch_dog_shutdown_event.wait(timeout=3)
self.logger.info('watch_dog stopped')
запуск по команде:
Код: Выделить всё
celery -A cell worker -l debug --pool gevent --concurrency=500Это действительно большая проблема,Если трудоемкая задача занимает слишком много времени и контрольный сигнал не приходит вовремя, я завершу задачу в другой службе.
Как я могу убедитесь, что поток демона работает нормально в то время, когда он предполагается выполнить?
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-celery
Мобильная версия