Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:25:45+00:00 2026-05-31T11:25:45+00:00

My python script creates alot of threads, they are all daemon threads, I find

  • 0

My python script creates alot of threads, they are all daemon threads, I find that I get an error saying “out of memory”.

How do I kill a daemon thread whilst my script/application is running?

I understand the concept of daemon threads, that they destroy themselves when my process(script or application) closes/finishes. But I want to kill some of my daemon threads whilst my script is still running to avoid the “out of memory” error.

Will my thread below kill itself when there are no more tasks in the queue?

class ParsePageThread(threading.Thread):
    THREAD_NUM = 0

    def __init__(self, _queue):
        threading.Thread.__init__(self)
        self.queue       = _queue

    def run(self):
        while(True):
            try:
                url = self.queue.get()
            except Queue.Empty,e:
                return # WILL this kill the thread?
            finally:
                self.queue.task_done()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:25:47+00:00Added an answer on May 31, 2026 at 11:25 am

    I’ll answer your second question first because it is easier. Yes, returning from the run method will indeed stop the thread. A detailed explanation is threading: Thread Objects doc.

    To stop a thread that is running before it’s natural completion you have to get a little more creative. There is no direct kill method on a thread object. What you need to do is use a shared variable to define the state of the thread.

    alive = True
    
    class MyThread(threading.Thread):
        def run():
            while(alive):
                #do work here
    

    In some other piece of code, when you detect a condition for stopping that thread, the other thread simply sets alive to False:

    alive = False
    

    This is a simple example, I’ll leave it to you to scale to multiple threads.

    DANGER

    This example works because reading and setting a boolean variable are atomic actions in python because of the Global Interpreter Lock. Here is an excellent tutorial for lower level python threading. You should stick to using the Queue object because that’s exactly what it’s for.

    If you do anything more than reading and setting simple variables from multiple threads you should use Locks or alternatively Reentrant Locks depending on your design and needs. Even something as simple as a compare and swap without a lock can cause problems in your program that are very difficult to debug.

    Another piece of advice for python multithreading is to never do any significant work in the interpreter thread. It should setup and start all the other threads and then sleep or wait on a condition object until the program exits. The reason for this is no other python thread can receive operating system signals. This means that no other thread can deal with Ctrl+C aka KeyboardInterrupt exceptions. It can be a good practice to have the main thread handle the KeyboardInterrupt exception and then set all the alive variables to False so you can exit your program quickly. This is especially helpful while developing so you don’t have to constantly kill things when you make a mistake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that creates and starts 3 threads, and then goes
I have a Python script that reads through a text csv file and creates
I'm working on a Python script that creates text files containing size/space information about
I have a python script that creates a list of lists of server uptime
I have a small in-house Python script for Linux that creates a /home/user/environ_script.sh file
I have a python script that is trying to create a directory tree dynamically
I'm trying to create a Python script that would : Look into the folder
My python script executes fine in Jail shell, putting out html which I can
I'm writing a python script to extract data out of our 2GB Apache access
I have an email account set up that triggers a python script whenever it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.