How to use threads in python
I want to use it for reading values in 17770 files and to add all of them at the end to one dictionary object. I have a machine with 8 cores.
This is the code
def run_item_preprocess ():
scores = {};
for i in range(1,17771):
filename1 = "x_" + str(i) + ".txt";
lines1 = open(filename1).readlines();
Item1 = {};
for line in lines1:
tokens = line.split(',');
Item1[int(tokens[1])] = int(tokens[2]);
for j in range(1,17771):
if j == i:
continue;
filename2 = "x_" + str(i) + ".txt";
lines2 = open(filename2).readlines();
Item2 = {};
for line in lines2:
开发者_运维知识库 tokens = line.split(',');
u = int(tokens[1]);
r = int(tokens[2]);
if u in Item1:
Item2[u] = r;
if i not in scores:
scores[i] = {};
scores[i]= (s(Item1,Item2),j);
Here is the wonderful multiprocessing module. It lets you parallelise code, using processes not threads. This will use all cores.
An important difference is that processes don't share memory; a queue will help with the reduce step.
Here's some good parts of the python library reference to start with.
http://docs.python.org/py3k/library/threading.html
http://docs.python.org/py3k/library/_thread.html
As for how to use threads effectively, I recommend you google 'python thread tutorial' or something like that.
How do you think that using threads would help with this?
Although Python supports threading, the standard implementation (CPython) executes only one thread at a time. Hence it's hard to see how this would make the process run faster, even on multiple cores.
(If you're using JPython or IronPython, though, this restriction doesn't apply.)
精彩评论