Python Forum
threadlocals are garbage collected before thread ends
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
threadlocals are garbage collected before thread ends
#1
I have a factory method that may be called from many threads. The method looks inside the thread locals for a certain object and if it doesn't exist, creates one and stores it in thread locals and then returns the same.

The object created thus has a __del__ method implemented for cleanup.

Here is a small code that explains what I am doing . My expectation is that, there are only two objects created by the factory one in main thread and other in the daemon thread.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import threading


class MysteryObject:
    def __init__(self, name):
        self.name = name

    def __del__(self):
        print('{} => {} destroyed'.format(self.__class__.__name__, self.name))


def get_thread_ident():
    current = threading.currentThread()
    return '_'.join([current.getName(), str(current.ident)])


def make_object():
    thread_ident = get_thread_ident()
    thread_locals = threading.local()
    if hasattr(thread_locals, 'mystery_object'):
        print('found mystery object in thread {}'.format(thread_ident))
        return thread_locals.mystery_object
    print('Creating mystery object in thread {}'.format(thread_ident))
    thread_locals.mystery_object = MysteryObject(thread_ident)
    return thread_locals.mystery_object


def test_threaded_object_maker(total_iteration):
    iteration = 0
    my_obj = make_object()
    while iteration < total_iteration:
        my_cur_obj = make_object()
        if id(my_cur_obj) != id(my_obj):
            print('{} Thread mystery object was GCed'.format(my_obj.name))
            my_obj = my_cur_obj
        time.sleep(2)
        iteration += 2


def test_object_maker_main():
    main_thread_mystery = make_object()
    threads = []
    for _ in range(1):
        threads.append(threading.Thread(target=test_threaded_object_maker, args=(5,)))
        threads[-1].start()
    for th in threads:
        th.join()
    main_thread_mystery2 = make_object()
    if id(main_thread_mystery) != id(main_thread_mystery2):
        print('Error main thread mystery object was GCed')


if __name__ == '__main__':
    test_object_maker_main()
But when I run the code I see ...
Output:
Creating mystery object in thread MainThread_140259104548672 Creating mystery object in thread Thread-1_140259096856320 Creating mystery object in thread Thread-1_140259096856320 Thread-1_140259096856320 Thread mystery object was GCed MysteryObject => Thread-1_140259096856320 destroyed Creating mystery object in thread Thread-1_140259096856320 Thread-1_140259096856320 Thread mystery object was GCed MysteryObject => Thread-1_140259096856320 destroyed Creating mystery object in thread Thread-1_140259096856320 Thread-1_140259096856320 Thread mystery object was GCed MysteryObject => Thread-1_140259096856320 destroyed MysteryObject => Thread-1_140259096856320 destroyed Creating mystery object in thread MainThread_140259104548672 Error main thread mystery object was GCed MysteryObject => MainThread_140259104548672 destroyed MysteryObject => MainThread_140259104548672 destroyed
I expect objects in thread locals to stick around until the thread ends. What am I missing?
I also created a stackoverflow post but haven't got any answers yet.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pywin32: Outlook connection ends with 'operation aborted' on one machine tstone 0 2,325 May-03-2022, 04:29 AM
Last Post: tstone
  Regex: a string does not starts and ends with the same character Melcu54 5 2,369 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  Running a few lines of code as soon as my timer ends nethatar 3 2,355 Feb-26-2021, 01:02 PM
Last Post: jefsummers
  Images are storing in RAM and don't get garbage collected MaxRicik 4 2,668 Jan-07-2021, 02:59 AM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Keep Application running after Python script ends PEGylated_User 0 1,944 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,393 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Why doesn't gc delete an object without forcing a garbage collection call? AlekseyPython 5 3,714 Mar-19-2019, 02:10 AM
Last Post: micseydel
  Script ends, does not start again phanegem 8 5,016 Mar-30-2017, 07:36 PM
Last Post: nilamo
  Detect if an integer ends in 0 or 5 birdieman 4 13,352 Jan-11-2017, 09:32 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020