0

I have a thread when it is run it has an infinite loop. When another thread is stopped, I wish to reset all the threads again, There threads have sockets that transfer data between each other. I am using

node1._Thread__stop()

function to stop the thread runing, but when I try to create a new thread with the same name:

node1 = node.node(8081,8082,token,1,"Node A",0)
node1.start()

It gives out an error

[Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted.

I am not sure if the stop function worked on the threads, because I have a timeout clause for a socket that if it doesn't receive anything to print timeout. This prints after the error occured.

What would be a better reset of a thread/socket

DustBunny
  • 860
  • 2
  • 11
  • 25

1 Answers1

0

I'm not sure about what are you looking for. However, I recommend you to have a look at this answer to a related question.

The important point is that isn't a good idea to terminate a thread when it's holding a resource such as a socket as in your case. The right way to do it would be to use any of the synchronization mechanisms (the answer referenced above uses an example with threading.Event, but it could be, for example, a threading.Condition if it suits your needs better) available to release the resource or, as it seems you need, reset some internal data to start from scratch.

I hope this helps.

Community
  • 1
  • 1
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • that is the first thing I looked at actually before writing this question, I thought that his answer was too complicated for what I wish to have. I just simply wish to completely terminate or delete thread and socket. I do not need to save anything. – DustBunny Feb 21 '12 at 22:57
  • @user1009731 If you don't explicitly close a socket, it will remain upon until a timer at operating system level expires. Hence, you won't be able to re-open the socket for a while. I'm afraid you'll need to implement some thread synchronization to close that socket even if you don't need to keep any state. – jcollado Feb 22 '12 at 07:31