question to start_new_thread in thread

T

Thomas Schmid

Hi there,

I wrote a tcp server which listens on a port. When he gets a new
connection, he starts a new thread like this:
thread.start_new_thread(self.ConnectionHandler, (conn,))
where conn is the socket for the connection.

After I started the server, I can see one running process under Linux,
which is ok. But once I connected to the Server, I always see two
processes where one is the parent of the other. I think this should be
normal behavier as long as the connection is established. But I also
see two processes when the connection is finished. So I thought that I
don't quit the thread for the connection correctly. Then I should see
three processes after I started a second connection, but I don't.
There are alwys just the two of them.

My question now is: does python make a thread pool where it does not
really quit the threads but instead reuses them if necessary? Or does
it need a second process to administrate threads?

bye

Thomas
 
P

Peter Hansen

Thomas said:
I wrote a tcp server which listens on a port. When he gets a new
connection, he starts a new thread like this:
thread.start_new_thread(self.ConnectionHandler, (conn,))
where conn is the socket for the connection.

After I started the server, I can see one running process under Linux,
which is ok. But once I connected to the Server, I always see two
processes where one is the parent of the other. I think this should be
normal behavier as long as the connection is established. But I also
see two processes when the connection is finished. So I thought that I
don't quit the thread for the connection correctly. Then I should see
three processes after I started a second connection, but I don't.
There are alwys just the two of them.

My question now is: does python make a thread pool where it does not
really quit the threads but instead reuses them if necessary? Or does
it need a second process to administrate threads?

I can't answer this other than to say stop using "thread" and start
using "threading". If you are using "threading" and your threads stop,
they disappear as you would expect. I don't think anyone is supposed
to use "thread" anymore... and it might solve your problems.

-Peter
 
T

Thomas Schmid

Peter Hansen said:
I can't answer this other than to say stop using "thread" and start
using "threading". If you are using "threading" and your threads stop,
they disappear as you would expect. I don't think anyone is supposed
to use "thread" anymore... and it might solve your problems.

-Peter

I tried it out and wrote instead of thread.start_new_thread this:
connThread = Thread(target=self.ConnctionHandler, args=(conn,))
connThread.start()
The result is exactly the same. First, I see only one process. But as
soon as I connect to the server, there are two processes which always
are there even if I disconnect. But still, there are never more then
two processes. I think the other process is a controle thread because
I found this in the documentation:
"There is a ``main thread'' object; this corresponds to the initial
thread of control in the Python program. It is not a daemon thread."

bye

Thomas
 
P

Peter Hansen

Thomas said:
I tried it out and wrote instead of thread.start_new_thread this:
connThread = Thread(target=self.ConnctionHandler, args=(conn,))
connThread.start()
The result is exactly the same. First, I see only one process. But as
soon as I connect to the server, there are two processes which always
are there even if I disconnect. But still, there are never more then
two processes. I think the other process is a controle thread because
I found this in the documentation:
"There is a ``main thread'' object; this corresponds to the initial
thread of control in the Python program. It is not a daemon thread."

So, logically, one would conlude that your thread never exits, even when
you disconnect. You'd better post code, or start sticking print statements
inside until you discover what is actually happening in your code.

Or replace the thread target with a function that does a simple time.sleep()
and observe the results. You should see the process exit, which ought to
prove to you that the problem is with your specific server code, and not
threads in Python.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top