On 10/18/2011 01:59 PM, Navneet wrote:
Hi,
I am trying to search a list for prime numbers but it's throwing me an
error at line no.25.
I am not able to figure what exactly is the prob
ne help ??? Error is this:
$ python "prime1 - Copy.py"
Unhandled exception in thread started by
Traceback (most recent call last):
File "prime1 - Copy.py", line 25, in findPrime
close failed in file object destructor:
Error in sys.excepthook:
program is below: Numberlist is number range from 1..1000
import sys
import threading
import thread
import time
class FindPno():
c = []
f = open("A;\Numberlist.txt")
for i in f:
c.append(i)
f.close()
##print len(c)
##Thread should start here
def __init__(self):
thread.start_new_thread(self.findPrime,(1,))
def findPrime(self,tid):
global tlock
## print "I am here"
tlock = thread.allocate_lock()
## print "I am here"
tlock.acquire()
## print "I am here"
for i1 in range(len(c)): ##this is the 25th line
for i2 in range(2,int(c[i1])):
if int(c[i1]) == 1:
print "I am here"
tlock.release()
break
if int(c[i1]) == 2:
print c
print "I am here"
tlock.release()
break
rem = int(c[i1])%i2
if rem == 0:
print "I am here"
tlock.release()
break
if i2 == int(c[i1])-1:
print int(c[i1]), "This is the Thread",tid
print "I am here"
tlock.release()
tlock.release()
if __name__ == '__main__':
a = FindPno()
I have no idea what you're trying to accomplish here. The code is far
too complex to solve the problem you've described, and very inefficient
besides. And threads won't help speed up a CPU-bound Python program of
this type.
I also have no idea how you got the traceback you showed. After I
cleaned up a lot of other stuff, I get the error:
Traceback (most recent call last):
File "prim.py", line 52, in <module>
a = FindPno()
File "prim.py", line 19, in __init__
self.findPrime(0)
File "prim.py", line 28, in findPrime
for i2 in range(2,int(c[i1])):
NameError: global name 'c' is not defined
And the problem with that is that the class attribute 'c' would need a
prefix to address it in a method. Simplest would be to say
self.c
each place you have c. or else just declare global c inside the class
scope.
Is this program transliterated from some other language? Or were there
some strange constraints in the original assignment?
After cleanup, the program does work, but I had to get rid of the thread
stuff to make it work here. As written, the program terminates before
the extra thread does any work.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor