daemon.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # dm_process为key,value为具体指令 如: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 'dm ' + 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. # 定时备份
  45. def backup():
  46. r = redis()
  47. c = 'dm_process'
  48. i = 0
  49. while 1:
  50. value = r.lpop(c)
  51. if value:
  52. g = command(value)
  53. popen(g)
  54. i = i+1
  55. if i >= 10:
  56. gevent.sleep(timeSleep)
  57. i = 0
  58. def handle():
  59. gevent.joinall([
  60. gevent.spawn(process),
  61. gevent.spawn(backup),
  62. ])
  63. handle()