Wednesday, October 28, 2009

Python threading: An experience as newbie

I was trying to use threading in my python program and as I am new in python I need to learn the Threading. I was googling for a quick example of python threading and I found the following program from here.

import threading
import datetime

class ThreadClass(threading.Thread):
    def run(self):
        now = datetime.datetime.now()
        print "%s says Hello World at time: %s" %(self.getName(), now)
       
for i in range(2):
    t = ThreadClass()
    t.start()
When I run it, its give the following error:
Traceback (most recent call last):
  File "threading.py", line 1, in
    import threading
  File "/home/murtoza/pyro/threading.py", line 4, in
    class ThreadClass(threading.Thread):
AttributeError: 'module' object has no attribute 'Thread'


I just copy the code from the net, and it should run perfectly but why its not running!!! Whats the problem in the code? I had no idea. I googled by the keyword AttributeError: 'module' object has no attribute 'Thread'and found this. Its the same error, same OS. And I got the solution in the second page.

What I did? I coped the code from net, made a file named threading.py and pasted the code. Then run it by $ python threading.py command. The compiler got confused. The first line of the
code is import threading . Which treading file compiler should import. The file I made or the file system has? Both file's name is threading.py . The priority is the file I made, and there is no Thread inside my file. Actually this file is not a library. So I just rename my file as threadTest.py and its work perfectly.

No comments: