The process completion process with multiple technologies fails with Linux

I just noticed a problem with terminating a process (from the multiprocessing library) of a method on Linux. I have an application that works with the multiprocessing library, but ... when I call the terminate function on Windows, everything works fine, on the other hand, Linux does not work with this solution. As a substitute for the killing process, I was forced to use

 os.system('kill -9 {}'.format(pid)) 

I know this is not too smart, but it works. So I'm just wondering why this code works on Windows, but on Linux it fails.

Example:

 from multiprocessing import Process import os process=Process(target=foo,args=('bar',)) pid=process.pid process.terminate() # works on Windows only ... os.sytem('kill -9 {}'.format(pid)) # my replacements on Linux 

My configuration: python 3.2.0 build 88445; Linux-2.6.32-Debian-6.0.4

This is a sample from my code. I hope this will be enough.

 def start_test(timestamp,current_test_suite,user_ip): global_test_table[timestamp] = current_test_suite setattr(global_test_table[timestamp], "user_ip", user_ip) test_cases = global_test_table[timestamp].test_cases_table test_cases = test_cases*int(global_test_table[timestamp].count + 1) global_test_table[timestamp].test_cases_table = test_cases print(test_cases) print(global_test_table[timestamp].test_cases_table) case_num = len(test_cases) Report.basecounter = Report.casecounter = case_num setattr(global_test_table[timestamp], "case_num", case_num) setattr(global_test_table[timestamp], "user_current_test", 0) try: dbobj=MySQLdb.connect(*dbconnector) dbcursor=dbobj.cursor() dbcursor.execute(sqlquery_insert_progress.format(progress_timestamp = str(timestamp), user_current_test = global_test_table[timestamp].user_current_tes$ except :... for i in range(case_num): user_row = global_test_table[timestamp] current_test_from_tests_table = user_row.test_cases_table[i] unittest.TextTestRunner(verbosity=2).run(suite(CommonGUI.get_address(CommonGUI,current_test_from_tests_table[1], current_test_from_tests_table[2], user$ global_test_table[timestamp].user_current_test = i + 1 try: dbobj=MySQLdb.connect(*dbconnector) dbcursor=dbobj.cursor() dbcursor.execute(sqlquery_update_progress.format(progress_timestamp = str(timestamp), user_current_test = global_test_table[timestamp].user_current$ except :... @cherrypy.expose() def start_test_page(self, **test_suite): timestamp = str(time.time()) user_ip = cherrypy.request.remote.ip if on_server(): sys.stdout=sys.stderr=open("/var/log/cherrypy/test_gui/{file}.log".format(file=timestamp),"a") current_test_suite = self.parse_result(**test_suite) #global_test_table[timestamp] = current_test_suite #setattr(global_test_table[timestamp], "user_ip", user_ip) user_test_process = Process(target=start_test, args=(timestamp,current_test_suite,user_ip)) users_process_table[timestamp] = user_test_process user_test_process.start() return '''{"testsuite_id" : "''' + str(timestamp) + '''"}''' @cherrypy.expose() def stop_test(self, timestamp): if timestamp in users_process_table: if on_server(): user_process_pid = users_process_table[timestamp].pid os.system("kill -9 " + str(user_process_pid)) else: users_process_table[timestamp].terminate() del users_process_table[timestamp] else: return "No process exists" 
+6
source share
2 answers

From docs :

stop ()

Complete the process. On Unix, this is done using SIGTERM Signal; on Windows TerminateProcess (). Please note that exit handlers, and finally offers, etc., will not be executed.

Please note that the streaming processes of the process will not be stopped - they will simply become orphans.

So, it looks like you should make sure your process processes the SIGTERM signal correctly.

Use signal.signal to install a signal handler.

To install a SIGTERM signal handler that simply exists, use:

 import signal import sys signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(1)) 

EDIT

The Python process usually ends in SIGTERM, I don’t know why your multiprocessor process does not end in SIGTERM.

+5
source

This is not a straightforward answer to your question, but since you are dealing with threads, it can also be useful for debugging these threads: fooobar.com/questions/2580 / ... I recently found an error in cherry using this code.

0
source

Source: https://habr.com/ru/post/915368/


All Articles