What is the process to kill a particular thread in python?

0 votes
Hi guys. For my school project, I have created a game that kinda uses multithreading. Now when a character dies, I want to kill off that thread. Is it possible to terminate a running thread without setting/checking any flags/semaphores/etc.?
Feb 4 in Python by ariaholic
• 7,340 points
54 views

1 answer to this question.

0 votes

A multiprocessing.Process can p.terminate()

In the cases where I want to kill a thread, but do not want to use flags/locks/signals/semaphores/events/whatever, I promote the threads to full blown processes. For code that makes use of just a few threads the overhead is not that bad.

E.g. this comes in handy to easily terminate helper "threads" which execute blocking I/O

The conversion is trivial: In related code replace all threading.Thread with multiprocessing.Processand all queue.Queue with multiprocessing.Queue and add the required calls of p.terminate() to your parent process which wants to kill its child p

Python doc

answered Feb 4 by charlie_brown
• 7,720 points

Related Questions In Python

0 votes
0 answers

What is the logic to check if a number is prime or not in python?

I am looking for a function with ...READ MORE

Aug 2 in Python by Waseem
• 4,480 points
13 views
+1 vote
1 answer

What is the correct order to learn concepts in Python for machine learning?

Machine Learning is a vast domain. It ...READ MORE

answered Jul 25, 2018 in Python by Anmol
• 3,620 points
63 views
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 56,520 points
70 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 views
+4 votes
6 answers
0 votes
1 answer

What is the preferred way to concatenate strings in python??

If the strings you are concatenating are ...READ MORE

answered Dec 20, 2018 in Python by charlie_brown
• 7,720 points
79 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
79 views