0

It may be a silly question, but it's been bothering me for a long time. When I create threads in a program, the number in the thread name keeps growing.

from threading import Thread, current_thread
from time import sleep


def worker():
    print(f'{current_thread().name} running')


for i in range(10):
    t = Thread(target=worker)
    t.start()
    sleep(.1)
    print(t.is_alive())

The output is:

Thread-1 (worker) running
False
Thread-2 (worker) running
False
Thread-3 (worker) running
...

My question is, if a program keeps creating threads, does that number go up infinitely? For example, use ThreadingTcpServer to create a multi-threaded socket server:

import socketserver
from threading import current_thread


class MyServer(socketserver.BaseRequestHandler):
    def handle(self):
        conn = self.request
        data = self.request.recv(1024)
        print(f'{current_thread().name} receive: {data.decode()}') 
        conn.sendall(data)


if __name__ == '__main__':
    server = socketserver.ThreadingTCPServer(('127.0.0.1', 9999), MyServer)
    server.serve_forever()

Each time a request is received, the number gets bigger:

Thread-1 receive: hello
Thread-2 receive: world
...

If the server runs forever, does the number go up infinitely? Will the memory usage be affected?

telecomshy
  • 63
  • 6
  • 2
    Does this answer your question? [When does Python reset the thread count?](https://stackoverflow.com/questions/60347082/when-does-python-reset-the-thread-count) – BrokenBenchmark Mar 20 '22 at 03:26
  • @BrokenBenchmark Thank you very much, The article explains why the numbers keep growing, it's helpful. But what I'm trying to figure out is, Is there a problem with growing numbers, Like a memory leak or something. – telecomshy Mar 20 '22 at 04:13
  • Do you have any reason to suspect that there's a memory leak? – BrokenBenchmark Mar 20 '22 at 04:14
  • No, I just watched the numbers get bigger and bigger, kind of scared. I didn't run a full test, I thought it would take a long time. – telecomshy Mar 20 '22 at 04:18
  • I think I'm worrying too much. :-) – telecomshy Mar 20 '22 at 04:23
  • 1
    If you're _really concerned_, you could use a memory profiler, but if you have no reason to suspect a memory leak, then I wouldn't bother. – BrokenBenchmark Mar 20 '22 at 04:27
  • 1
    You're right. I should do some tests instead of imagining things. Thanks again. – telecomshy Mar 20 '22 at 04:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243106/discussion-between-telecomshy-and-brokenbenchmark). – telecomshy Mar 20 '22 at 04:44

1 Answers1

0

I reconsidered my question,I think I got a few things wrong at first.

  1. The number at the end of the name simply marks the thread, whether it is active or not. Explain is here: When does Python reset the thread count?, thanks BrokenBenchmark.

  2. I thought sys.maxsize is maximum int in Python. If the number of thread larger than that, maybe have some problem. but now I knew I was wrong, see this answer: Max integer value of python3. At the same time, there is no need to worry about memory usage, even 10**1000, sys.getsizeof(10**1000), takes only 468 bytes.

telecomshy
  • 63
  • 6