Starting threads

Andrew Bennetts andrew-pythonlist at
Thu Jan 30 04:57:34 CET 2003

On Wed, Jan 29, 2003 at 08:51:13AM +0100, Lukas Kubin wrote:
> Thank you for answer. OK, I will describe it better.
> The doTheAction() function is defined in the Control class and looks
> almost like this:
> def sendSignal(address,port,command):
>   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>   try:
>     s.connect((address, port))
>   except socket.error, msg:
>     s = None
>     return "false"
>   s.send(command)
>   # Here follows processing of returned values,etc.
>   s.close()

This is a complete jump sideways from your thread problems, but for handling
multiple simultaneous socket connections, you will probably find using
Twisted <> is easier than trying to do it yourself.

> The Control class is described in my previous letter bellow and the
> Computer (it is not class I just badly shorted it for example) object is a
> list of hostnames. So the main call looks like:
> if __name__ == '__main__':
>   for comp in ['hostname1','hostname2']:
>     thread = Control(comp)
>     thread.start()
>     time.sleep(.005)
> When I don't use the time.sleep() then it starts 2 threads of 'hostname2'.
> Thank you

What exactly do you mean by "2 threads of 'hostname2'"?  Both threads
connect to hostname2?

I strongly suspect you have a bug in your Control class of it sharing the
hostname passed into the constructor between all instances, thus causing all
of your threads to use/connect/something that hostname.  You might be using
" = foo" instead of " = foo", or you might be sharing a
mutable between instances without realising, e.g. with:

class Foo:
    shared = []  # Lists are mutable, so changing this in one instance
                 # affects all other instances


More information about the Python-list mailing list