Skip to content

Commit d077f42

Browse files
Merge pull request #281 from matthewdavislee/master
Revised patch from commit ea4ce60
2 parents effa659 + 847ff4f commit d077f42

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

tika/tika.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def make_content_disposition_header(fn):
143143
import signal
144144
import logging
145145
import io
146+
import ctypes
146147

147148
log_path = os.getenv('TIKA_LOG_PATH', tempfile.gettempdir())
148149
log_file = os.path.join(log_path, 'tika.log')
@@ -665,9 +666,15 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
665666

666667
# Run java with jar args
667668
global TikaServerProcess
669+
# Patch for Windows support
668670
if Windows:
669-
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, start_new_session=True)
670-
else:
671+
if sys.version.startswith("2"):
672+
# Python 2.x
673+
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True)
674+
elif sys.version.startswith("3"):
675+
# Python 3.x
676+
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, start_new_session=True)
677+
else:
671678
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, preexec_fn=os.setsid)
672679

673680
# Check logs and retry as configured
@@ -694,8 +701,22 @@ def killServer():
694701
Kills the tika server started by the current execution instance
695702
'''
696703
if(TikaServerProcess):
697-
os.killpg(os.getpgid(TikaServerProcess.pid), signal.SIGTERM)
698-
time.sleep(1)
704+
# patch to support subprocess killing for windows
705+
if Windows:
706+
if sys.version.startswith("2"):
707+
# Python 2.x
708+
PROCESS_TERMINATE = 1
709+
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, TikaServerProcess.pid)
710+
ctypes.windll.kernel32.TerminateProcess(handle, -1)
711+
ctypes.windll.kernel32.CloseHandle(handle)
712+
time.sleep(1)
713+
elif sys.version.startswith("3"):
714+
# Python 3.x
715+
os.kill(TikaServerProcess.pid, signal.SIGTERM)
716+
time.sleep(1)
717+
else:
718+
os.killpg(os.getpgid(TikaServerProcess.pid), signal.SIGTERM)
719+
time.sleep(1)
699720
else:
700721
log.error("Server not running, or was already running before")
701722

0 commit comments

Comments
 (0)