3

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()
2
  • Also, why can't you use a Queue, or 2 Queues? Commented Mar 23, 2014 at 18:55
  • 2
    I am Serbian, and used to write names of functions in Serbian. Sorry 'bout that. Commented Mar 24, 2014 at 19:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried with SIGTERM but it didn't recognize signal.SIGTERM so i thought if there is another way to do that. The other problem was getting PID from processes levi and desni to process koci.
@JaSamSale, Post your code to any online service (pastebin.com for instance) and we'll try to find a workaround.
I found a way to work around it, but now I wanna know why this doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.