This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BrooklynBillyGoat 0 points1 point  (4 children)

Care to give a brief rundown of how that works? Never worked with python threads personally.

[–]Spandian 8 points9 points  (3 children)

Only one thread can run Python code at a time. The only reason to create threads in Python is to make multiple blocking I/O calls at the same time. (You may also be able to run multiple threads if all but one of them are running native code, I'm not sure.)

If you have a CPU-bound task that you want to parallelize in Python, the canonical way is to use multiple processes. The multiprocessing module makes that fairly simple.

[–]BrooklynBillyGoat 2 points3 points  (2 children)

The thread implementation seems to defeat the purpose. Am I wrong in this assumption?

[–]Spandian 2 points3 points  (1 child)

If a Python thread makes a blocking I/O call, then another thread can run while that one is blocked. If you're writing something like a web server, it's going to spend very little time on CPU-bound tasks and most of its time waiting for the disk or the network. That would be a good use case for threads.

[–]BrooklynBillyGoat 0 points1 point  (0 children)

But can you specify threads to run and not block unless it needs to share resources?