master_cron.py 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # 用于批量处理转换 运行多个转换容器
  4. import time
  5. import subprocess
  6. import os
  7. import pprint
  8. timeSleep = 15
  9. def redis(self):
  10. import redis
  11. host = '0.0.0.0'
  12. port = 6379
  13. pool = redis.ConnectionPool(host=host, port=int(port))
  14. return redis.Redis(connection_pool=pool)
  15. def command(file):
  16. return 'dm call office-convert_call id=' + file
  17. def popen(command, bg=False):
  18. string = command
  19. if bg == True:
  20. command = command + ' 1>/dev/null 2>&1 &'
  21. popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  22. output = ''
  23. Demeter.echo(string)
  24. while True:
  25. output = popen.stdout.readline()
  26. pprint.pprint(output)
  27. if popen.poll() is not None:
  28. break
  29. return output
  30. # 文档转换
  31. def convert():
  32. r = redis()
  33. c = 'office_file'
  34. i = 0
  35. while 1:
  36. file = r.lpop(c)
  37. if file:
  38. g = command(file)
  39. Shell.popen(g, True)
  40. i = i+1
  41. if i >= 10:
  42. time.sleep(timeSleep)
  43. i = 0
  44. def handle():
  45. convert()
  46. handle()