in Education by
Is it possible to terminate a running thread without setting/checking any flags/semaphores/etc.? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Yes it is possible to terminate a running thread but before terminating or killing thread you should think of the following cases:- Thread holds a critical resource that must be closed properly. When we create one thread it creates several threads that must be killed. So for handling this problem, we have a very nice way exit_request flag so that at regular intervals each thread check if it is time for it to exit. Let’s understand this with an example:- import threading class StoppableThread(threading.Thread): def __init__(self): super(StoppableThread, self).__init__() self._stop_event = threading.Event() def stop(self): self._stop_event.set() def stopped(self): return self._stop_event.is_set() In this above code, you should call stop()on the thread when you want it to exit, and wait for the thread to exit properly using join(). The thread should check the stop flag at regular intervals.

Related questions

0 votes
    I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the ... same thing? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    So, I am creating an application which displays song lyrics for entered songs, however, longer songs do not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I wrote the code for the counter in python, but I did not my appropriate result. I made the list by input numbers, ... like: [33,16,19] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am using Eclipse with Pydev and googleAppengine. I have python 2.7 installed. I am trying to run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    A number of cats got together and decided to kill between them 999919 rats. Every cat killed an equal number of rats. Each cat ... cat killed ? A) 1009 B) 991 C) 2000 D) 1000...
asked Feb 12, 2021 in Education by JackTerrance
0 votes
    I have a pdf template file that contains one form field. I want to fill out and flatten this form ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    So, I started learning to code in Python and later Django. The first time it was hard looking at tracebacks and ... your Django code? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I have recently started studying Python, but I couldn't find how to implement multi-line comments. Most languages ... comment feature? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am using autoencoders to do anomaly detection. So, I have finished training my model and now I want to ... y _true and y_pred Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever ... is it necessary? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a code: ab = 'off' if b.page == 'blog': ab = 'on' print ab Is there any other way to make it shorter in Python? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I am searching for the better option to save a trained model in PyTorch, these are the options I am using ... the above two functions? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I check a given object is of a given type? How can I check that a object inherit a given type? There's ... that it's a string? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...