Python and threads

Diez B. Roggisch deets at nospam.web.de
Sun Jan 18 11:48:49 EST 2009


vedrandekovic at yahoo.com schrieb:
> Hello again,
> 
> Thanks for previous help on "Start two threads in same time" it was
> useful,but when I run this
> two threads, I think they don't start at the same time, here is my
> code snippet:
> 
> 
> import threading
> 
> class ThreadedClass1(threading.Thread):
>     def __init__(self):
>         threading.Thread.__init__(self)
> 
>     def run(self):
>         a=True
>         while a==True:
>             bm=my_module.MyClass()
>             a=bm.get_Python_Process_Usage_Function()   #Returns True
> or False
> 
> class ThreadedClass2(threading.Thread):
>     def __init__(self):
>         threading.Thread.__init__(self)
> 
>     def run(self):
>         os.popen("my_python_script.py")
> 
> 
> 
> threaded_obj = ThreadedClass1()
> threaded_obj.start()
> threaded_obj2 = ThreadedClass2()
> threaded_obj2.start()

If you want to synchronize two or more threads, you need to do so 
manually using objects like Locks, Events, Conditions and Semaphores. 
See the threading module.

Even if you managed to get two threads started simultaneously (which the 
  OS doesn't even offer IINM), the would soon run out of sync.

How about you tell us what you *want*, and we tell you if and how it's 
possible to do that.

Diez




More information about the Python-list mailing list