daemon.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # dm_process为key,value为具体指令 如:dm call office-convert_call id=1
  4. import time
  5. import os
  6. #import pprint
  7. #import subprocess
  8. from gevent import monkey; monkey.patch_socket()
  9. import gevent
  10. timeSleep = 15
  11. password = 'dm_redis_123'
  12. def redis():
  13. import redis
  14. host = '0.0.0.0'
  15. port = 6379
  16. pool = redis.ConnectionPool(host=host, port=int(port), password=password)
  17. return redis.Redis(connection_pool=pool)
  18. def pop(key):
  19. return popen('redis -a '+password+' lpop ' + key)
  20. def command(process):
  21. return process
  22. def popen(command, bg=False):
  23. string = command
  24. if bg == True:
  25. command = command + ' &'
  26. process = os.popen(command)
  27. output = process.read()
  28. process.close()
  29. return output
  30. # 定时执行进程
  31. def process():
  32. r = redis()
  33. c = 'dm_process'
  34. i = 0
  35. while 1:
  36. value = r.lpop(c)
  37. if value:
  38. g = command(value)
  39. popen(g)
  40. i = i+1
  41. if i >= 10:
  42. gevent.sleep(timeSleep)
  43. i = 0
  44. def handle():
  45. gevent.joinall([
  46. gevent.spawn(process),
  47. #gevent.spawn(backup),
  48. ])
  49. handle()