简体   繁体   中英

Kill process using another process python multiprocessing

I'm trying to create a script in Python. The idea is to start 3 processes, 2 of them constantly print a message, and the third is there to kill them after a few seconds. The problem is that I don't know how to tell that third which processes should be terminated.

from multiprocessing import *
import time

def OkreciLevi():
   while 1:
       print "okrecem levi"
       time.sleep(3)

def OkreciDesni():
   while 1:
       print "okrecem desni"
       time.sleep(3)

def Koci(levi,desni):
   for vrednost in range(2):
       print str(vrednost)
       time.sleep(3)
   levi.terminate()
   desni.terminate()
   print "kocim"

if __name__== '__main__':
   levi=Process(target=OkreciLevi)
   desni=Process(target=OkreciDesni)
   koci=Process(target=Koci, args=(levi,desni))
   koci.start()
   levi.start()
   desni.start()
   levi.join()
   desni.join()
   koci.join()

Assuming that you're on *nix-like operating system I guess that you need to:

  1. Get the PID of the multiprocessing worker;
  2. Send SIGTERM to them. For instanse use os.kill .

Also this information may be useful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM