Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 483
JaSamSale Avatar asked Apr 16 '26 01:04

JaSamSale


1 Answers

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.

like image 97
Vitaly Isaev Avatar answered Apr 18 '26 14:04

Vitaly Isaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!