1

So I wrote a python that will find what the pin the users inputs, everything works fine except one thing, and that's that the script won't exist once the pin is found. Is there a why I can kill all of the other threads once I found the pin?

#!/usr/bin/env python 
#
#
#
from threading import Thread
from random import randint 
from time import sleep 
from sys import exit
from os import system

system('clear');sleep(0.7)
Pin = int(raw_input('Enter a pin: '))


def Up():
    global Pin
    for pin in xrange(1111,10000):
        system('clear')
        print pin
        if pin == Pin:
            system('clear')
            print 'U Pin Found: %d'%pin;sleep(0.7)
            for i in range(3):
                exit()


def Down():
    global Pin
    pins = xrange(10000)
    for pin in reversed(pins):
        system('clear')
        print pin
        if pin == Pin:
            system('clear')
            print 'D Pin Found: %d'%pin;sleep(0.7)
            exit()



def Random():
    global Pin

    while True:
        pins = randint(1111,10000)
        print pins
        if pins == Pin:
           system('clear')
           print 'R Pin Found: %d'%pins;sleep(0.7)
           exit()


Task1 = Thread(target=Up,args=())
Task2 = Thread(target=Down,args=())
Task3 = Thread(target=Random,args=())


Task1.start()
Task2.start()
Task3.start()
  • Off-topic: The way you're clearing the screen isn't very portable. Instead of `os.system('clear')` consider using `os.system('cls' if os.name == 'nt' else 'clear')`, which works on both Windows and Linux. – martineau Jun 29 '16 at 14:09
  • @martineau And I wouldn't do it at all. I don't see the point in doing so. – glglgl Jun 29 '16 at 14:17
  • @glglgl: My comment wasn't addressed to you. The point is _portability_. It would allow folks on other operating systems to easily run the code (and maybe provide a good answer the question). – martineau Jun 30 '16 at 14:30
  • @martineau Even if a comment isn't addressed at me, I felt free to comment on this topic as well. After all, it is a system where everyone can add comments at their will. You are completely right concerning portability: if one really needs clearing the screen, it is better to do it portably. But I personally feel programs which clear the screen over and over again just annoying, what I tried to express with this. (And I personally would prefer `subprocess` over `os.system()`, but that's another point.) – glglgl Jun 30 '16 at 14:46

2 Answers2

1

You can't simply terminate them. However you can ask to stop. Please see example:

from threading import Thread
from random import randint 
from time import sleep 
from sys import exit
from os import system

system('clear');sleep(0.7)
Pin = int(raw_input('Enter a pin: '))

terminateAll = false

def Up():
    global Pin
    for pin in xrange(1111,10000):
        if terminateAll:
            exit()
        system('clear')
        print pin
        if pin == Pin:
            terminateAll = true
            system('clear')
            print 'U Pin Found: %d'%pin;sleep(0.7)
            for i in range(3):
                exit()


def Down():
    global Pin
    pins = xrange(10000)
    for pin in reversed(pins):
        if terminateAll:
            exit()
        system('clear')
        print pin
        if pin == Pin:
            terminateAll = true
            system('clear')
            print 'D Pin Found: %d'%pin;sleep(0.7)
            exit()



def Random():
    global Pin

    while True:
        if terminateAll:
            exit()
        pins = randint(1111,10000)
        print pins
        if pins == Pin:
           terminateAll = true
           system('clear')
           print 'R Pin Found: %d'%pins;sleep(0.7)
           exit()


Task1 = Thread(target=Up,args=())
Task2 = Thread(target=Down,args=())
Task3 = Thread(target=Random,args=())


Task1.start()
Task2.start()
Task3.start()
Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
0

All you need to do is make the thread objects daemons by setting their daemon to attribute to True before starting them.

The default value is False. Any daemon threads still running when the main program thread ends will automatically be terminated.

A logical place to do this is right after calling their constructor:

  ...
Task1 = Thread(target=Up,args=())
Task1.daemon = True
Task2 = Thread(target=Down,args=())
Task2.daemon = True
Task3 = Thread(target=Random,args=())
Task3.daemon = True

Task1.start()
Task2.start()
Task3.start()
Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301