python_yndexer.py 1.3 KB

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