python_yndexer.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import threading
  5. import subprocess
  6. def _try_to_kill(process):
  7. try:
  8. process.kill()
  9. except Exception:
  10. pass
  11. def touch(path):
  12. if not os.path.exists(path):
  13. with open(path, 'w') as _:
  14. pass
  15. class Process(object):
  16. def __init__(self, args):
  17. self._process = subprocess.Popen(args)
  18. self._event = threading.Event()
  19. self._result = None
  20. thread = threading.Thread(target=self._run)
  21. thread.setDaemon(True)
  22. thread.start()
  23. def _run(self):
  24. self._process.communicate()
  25. self._result = self._process.returncode
  26. self._event.set()
  27. def wait(self, timeout):
  28. self._event.wait(timeout=timeout)
  29. _try_to_kill(self._process)
  30. return self._result
  31. if __name__ == '__main__':
  32. yndexer = sys.argv[1]
  33. timeout = int(sys.argv[2])
  34. output_file = sys.argv[3]
  35. input_file = sys.argv[4]
  36. partition_count = sys.argv[5]
  37. partition_index = sys.argv[6]
  38. process = Process([yndexer, '-f', input_file, '-y', output_file, '-c', partition_count, '-i', partition_index])
  39. result = process.wait(timeout=timeout)
  40. if result != 0:
  41. print('Yndexing process finished with code', result, file=sys.stderr)
  42. touch(output_file)