0

I am generating rather large images, and so I want to thread it to render different parts of the image at once.

for startX in xrange(0, width, threadChunk):
    t = Thread(target = threaded_chunk, args = (thread_num, startX, startX + threadChunk, 1, 0, height, 1))
    t.start()
    t.join()
    print 'next thread'
    thread_num += 1

However, this doesn't seem to do what I think it should --- it seems to me that it waits for one thread to finish before going onto the next one. I found this out because my actual threaded function (threaded_chunk), has a print statement at the end, which notifies me that the thread has finished. The shell looks like this when I run the program:

Thread 1 completed
next thread
Thread 2 completed
next thread
# etc

Therefore, it is waiting for one to finish before running the next.

I am using t.join() because I want to execute a final step after all of the threads have finished: image.save(imagepath).

Is there a way to run numerous threads at once but then wait until all of the threads have finished to execute a task? Thanks!

EDIT: My current method works on Ubuntu 12.04 LTS, but not Windows 8.1.

1
  • If I'm not mistaken, based on answers to a similar question I had, I think in order for the multiple rendering threads to run simultaneously you have to make sure that the function you're sending to be threaded calls C code directly. That is, it should call PIL.Image.polygon (or whatever rendering module/operation you're using) instead of threadedchunk which I assume is a function you defined on your own using Python code. I could be wrong though, so curious to hear your results. Commented Jan 18, 2014 at 19:45

1 Answer 1

2

You should first start all threads, and then wait for them. You can do it like this:

threads = []
for startX in xrange(0, width, threadChunk):
    t = Thread(target = threaded_chunk, args = (thread_num, startX, startX + threadChunk, 1, 0, height, 1))
    threads.append(t)
    print 'next thread'

[t.start() for t in threads]
[t.join() for t in threads]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.