JaSamSale
JaSamSale

Reputation: 169

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()

Upvotes: 3

Views: 1738

Answers (1)

Vitaly Isaev
Vitaly Isaev

Reputation: 5815

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.

Upvotes: 1

Related Questions