loader.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. from .__load__ import *
  3. from gevent import monkey; monkey.patch_socket()
  4. import gevent
  5. class Loader(object):
  6. # 获取
  7. def get(self, obj='', module='', **param):
  8. if 'sync' in param and param['sync']:
  9. return Demeter.service(obj, module).get(**param)
  10. else:
  11. param['sync'] = False
  12. id = Demeter.service(obj, module).get(**param)
  13. redis = Demeter.redis()
  14. config = Demeter.config['redis']
  15. content = obj + '|' + module + '|' + str(id)
  16. redis.rpush(config['name'], content)
  17. return id
  18. # 启动任务
  19. def start(self):
  20. gevent.joinall([
  21. gevent.spawn(self.run),
  22. ])
  23. # 定时运行异步任务
  24. def run(self):
  25. timeSleep = 1
  26. redis = Demeter.redis()
  27. config = Demeter.config['redis']
  28. i = 0
  29. while 1:
  30. content = redis.lpop(config['name'])
  31. if content:
  32. print(content)
  33. command = self.command(content.decode('utf-8'))
  34. Shell.popen(command, False, False)
  35. i = i+1
  36. if i >= 10:
  37. gevent.sleep(timeSleep)
  38. i = 0
  39. def command(self, content):
  40. temp = content.split('|')
  41. python_path = sys.executable
  42. loader_path = os.path.join(File.path(), 'loader.py') # 生成系统规范路径
  43. return f'"{python_path}" "{loader_path}" -o {temp[0]} -m {temp[1]} -i {temp[2]}'