common.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding=utf-8
  2. """
  3. @project: maxkb
  4. @Author:虎
  5. @file: common.py
  6. @date:2023/11/10 10:41
  7. @desc:
  8. """
  9. from concurrent.futures import ThreadPoolExecutor
  10. from django.core.cache.backends.locmem import LocMemCache
  11. work_thread_pool = ThreadPoolExecutor(5)
  12. embedding_thread_pool = ThreadPoolExecutor(3)
  13. memory_cache = LocMemCache('task', {"OPTIONS": {"MAX_ENTRIES": 1000}})
  14. def poxy(poxy_function):
  15. def inner(args, **keywords):
  16. work_thread_pool.submit(poxy_function, args, **keywords)
  17. return inner
  18. def get_cache_key(poxy_function, args):
  19. return poxy_function.__name__ + str(args)
  20. def get_cache_poxy_function(poxy_function, cache_key):
  21. def fun(args, **keywords):
  22. try:
  23. poxy_function(args, **keywords)
  24. finally:
  25. memory_cache.delete(cache_key)
  26. return fun
  27. def embedding_poxy(poxy_function):
  28. def inner(*args, **keywords):
  29. key = get_cache_key(poxy_function, args)
  30. if memory_cache.has_key(key):
  31. return
  32. memory_cache.add(key, None)
  33. f = get_cache_poxy_function(poxy_function, key)
  34. embedding_thread_pool.submit(f, args, **keywords)
  35. return inner