[Tutor] threading mind set
steve at pearwood.info
Sun May 13 02:22:04 CEST 2012
carlo locci wrote:
> Hello All,
> I've started to study python a couple of month ago(and I truly love it :)),
> however I'm having some problems understanding how to modify a sequential
> script and make it multithreaded (I think it's because I'm not used to
> think in that way),
No, that's because multithreading and parallel processing is hard.
> as well as when it's best to use it(some say that
> because of the GIL I won't get any real benefit from threading my script).
That depends on what your script does.
In a nutshell, if your program is limited by CPU processing, then using
threads in Python won't help. (There are other things you can do instead, such
as launching new Python processes.)
If your program is limited by disk or network I/O, then there is a possibility
you can speed it up with threads.
> It's my understanding that threading a program in python can be useful when
> we've got some I/O involved,
To see the benefit of threads, it's not enough to have "some" I/O, you need
*lots* of I/O. Threads have some overhead. Unless you save at least as much
time as just starting and managing the threads consumes, you won't see any
In my experience, for what little it's worth [emphasis on "little"], unless
you can keep at least four threads busy doing separate I/O, it probably isn't
worth the time and effort. And it's probably not worth it for trivial scripts
-- who cares if you speed your script up from 0.2 seconds to 0.1 seconds?
But as a learning exercise, sure, go ahead and convert your script to threads.
One experiment is worth a dozen opinions.
You can learn more about threading from here:
By the way, in future, please don't decorate your code with stars:
> * def read():*
> * import csv*
> * with open('C:\\test\\VDB.csv', 'rb') as somefile:*
We should be able to copy and paste your code and have it run immediately, not
have to spend time editing it by hand to turn it back into valid Python code
that doesn't give a SyntaxError on every line.
See also this: http://sscce.org/
More information about the Tutor