This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
portail-citoyen2/portail_citoyen2/apps/feed_plugin/utils.py

24 lines
591 B
Python

import logging
import threading
from django.core.cache import cache
logger = logging.getLogger(__name__)
def launch_in_thread(key, function, *args, **kwargs):
'''Launch a function in a thread, prevent launching the same function many times using a lock'''
key = 'thread-' + key
created = cache.add(key, 1)
if created:
logger.debug('launching thread for %s', key)
def f():
try:
function(*args, **kwargs)
finally:
cache.delete(key)
thread = threading.Thread(target=f)
thread.start()