@@ -140,6 +140,7 @@ def make_content_disposition_header(fn):
140140from subprocess import Popen
141141from subprocess import STDOUT
142142from os import walk
143+ import signal
143144import logging
144145import io
145146
@@ -188,6 +189,9 @@ def make_content_disposition_header(fn):
188189EncodeUtf8 = 0
189190csvOutput = 0
190191
192+ # will be used later on to kill the process and free up ram
193+ TikaServerProcess = False
194+
191195class TikaException (Exception ):
192196 pass
193197
@@ -660,7 +664,8 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
660664 return False
661665
662666 # Run java with jar args
663- cmd = Popen (cmd_string , stdout = logFile , stderr = STDOUT , shell = True )
667+ global TikaServerProcess
668+ TikaServerProcess = Popen (cmd_string , stdout = logFile , stderr = STDOUT , shell = True , preexec_fn = os .setsid )
664669
665670 # Check logs and retry as configured
666671 try_count = 0
@@ -681,6 +686,16 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
681686 else :
682687 return True
683688
689+ def killServer ():
690+ '''
691+ Kills the tika server started by the current execution instance
692+ '''
693+ if (TikaServerProcess ):
694+ os .killpg (os .getpgid (TikaServerProcess .pid ), signal .SIGTERM )
695+ time .sleep (1 )
696+ else :
697+ log .error ("Server not running, or was already running before" )
698+
684699def toFilename (url ):
685700 '''
686701 gets url and returns filename
0 commit comments