python – Linux上的多处理进程终止失败

前端之家收集整理的这篇文章主要介绍了python – Linux上的多处理进程终止失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我刚刚注意到Linux上进程终止(来自多处理库)方法的问题.我有应用程序使用多处理库,但…当我在Windows上调用终止函数时,一切都很好,另一方面,Linux失败了这个解决方案.作为过程杀戮的替代,我被迫使用

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

我知道这不是太聪明,但它确实有效.所以我只是想知道为什么这段代码在Windows上运行,但在Linux上运行失败.

例:

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

我的配置:python 3.2.0 build 88445; Linux的2.6.32-Debian的6.0.4

这是我的代码中的示例.我希望这就足够了.

  1. def start_test(timestamp,current_test_suite,user_ip):
  2. global_test_table[timestamp] = current_test_suite
  3. setattr(global_test_table[timestamp],"user_ip",user_ip)
  4. test_cases = global_test_table[timestamp].test_cases_table
  5. test_cases = test_cases*int(global_test_table[timestamp].count + 1)
  6. global_test_table[timestamp].test_cases_table = test_cases
  7. print(test_cases)
  8. print(global_test_table[timestamp].test_cases_table)
  9. case_num = len(test_cases)
  10. Report.basecounter = Report.casecounter = case_num
  11. setattr(global_test_table[timestamp],"case_num",case_num)
  12. setattr(global_test_table[timestamp],"user_current_test",0)
  13. try:
  14. dbobj=MysqLdb.connect(*dbconnector)
  15. dbcursor=dbobj.cursor()
  16. dbcursor.execute(sqlquery_insert_progress.format(progress_timestamp = str(timestamp),user_current_test = global_test_table[timestamp].user_current_tes$
  17. except :...
  18. for i in range(case_num):
  19. user_row = global_test_table[timestamp]
  20. current_test_from_tests_table = user_row.test_cases_table[i]
  21. unittest.TextTestRunner(verbosity=2).run(suite(CommonGUI.get_address(CommonGUI,current_test_from_tests_table[1],current_test_from_tests_table[2],user$
  22. global_test_table[timestamp].user_current_test = i + 1
  23. try:
  24. dbobj=MysqLdb.connect(*dbconnector)
  25. dbcursor=dbobj.cursor()
  26. dbcursor.execute(sqlquery_update_progress.format(progress_timestamp = str(timestamp),user_current_test = global_test_table[timestamp].user_current$
  27. except :...
  28. @cherrypy.expose()
  29. def start_test_page(self,**test_suite):
  30. timestamp = str(time.time())
  31. user_ip = cherrypy.request.remote.ip
  32. if on_server():
  33. sys.stdout=sys.stderr=open("/var/log/cherrypy/test_gui/{file}.log".format(file=timestamp),"a")
  34. current_test_suite = self.parse_result(**test_suite)
  35. #global_test_table[timestamp] = current_test_suite
  36. #setattr(global_test_table[timestamp],user_ip)
  37. user_test_process = Process(target=start_test,args=(timestamp,user_ip))
  38. users_process_table[timestamp] = user_test_process
  39. user_test_process.start()
  40. return '''{"testsuite_id" : "''' + str(timestamp) + '''"}'''
  41. @cherrypy.expose()
  42. def stop_test(self,timestamp):
  43. if timestamp in users_process_table:
  44. if on_server():
  45. user_process_pid = users_process_table[timestamp].pid
  46. os.system("kill -9 " + str(user_process_pid))
  47. else:
  48. users_process_table[timestamp].terminate()
  49. del users_process_table[timestamp]
  50. else:
  51. return "No process exists"
最佳答案
docs

terminate()

Terminate the process. On Unix this is done using the
SIGTERM signal; on Windows TerminateProcess() is used. Note that exit
handlers and finally clauses,etc.,will not be executed.

Note that descendant processes of the process will not be terminated –
they will simply become orphaned.

所以看起来你必须确保你的进程正确处理SIGTERM信号.

使用signal.signal设置信号处理程序.

要设置一个只存在该进程的SIGTERM信号处理程序,请使用:

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

编辑

Python进程通常在SIGTERM上终止,我不知道为什么你的多处理进程不会在SIGTERM上终止.

猜你在找的Python相关文章