thread stomp?

P

pyn3wb

class1 (threading.Thread):
def __init__(self, dir):
self.dir = dir
threading.Thread.__init__(self)

def run(self):
do stuff in dir here...

class2 (threading.Thread):
def __init__(self, dir, dir2):
self.file = g
self.dir2 = dir2
threading.Thread.__init__(self)

def run(self):
do stuff in dir here...
output to dir2

class main
for i in dircache.listdir(dir):
//run over files
class1(dir).start()

for j in dircache.listdir(dir2):
//run over files, dependent on stuff in dir
class2(dir2).start()

think just using the loops above enough to prevent class2 threads from
starting before class1 threads done

not true

class2 threads seems to start prior to class1 threads done; class2
results depend on class1 finish first

any help?
 
D

D'Arcy J.M. Cain

class1 (threading.Thread):
^
SyntaxError: invalid syntax

Give us a script that works. Ideally it should output something that
indicates what the error is.
//run over files

This is not a comment in Python.
class2 threads seems to start prior to class1 threads done; class2
results depend on class1 finish first

What are you trying to do? If all you care about is making one thread
depend on another finishing, why make two threads? Just have one
thread do one thing and then do the other.

Not every problem is a threading problem.
 
J

Jerry Hill

class main

Why are you making main a class? It should almost certainly be a
function (defined with "def", not "class").
for i in dircache.listdir(dir):
//run over files
class1(dir).start()

You've started a bunch of threads. If you want to wait for them to
all finish, you need to call .join() on them all. You can't do that
unless you save references to your threads, like this:

threads = []
for i in dircache.listdir(dir):
new_thread = class1(dir)
new_thread.start()
threads.append(new_thread)

for thread in threads:
thread.join()

Beyond that, the code you've posted is not correct. You've left off a
required ":" in your definition of class main, and your comments are
not valid python comments. Your for loops don't make a whole lot of
sense to me either. Are they supposed to be iterating across files in
a directory? If so, why do you start up all of your threads with
"dir" instead of "i" or "j" as the parameter to your new thread?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top