简体   繁体   English

如何在python中使用线程

[英]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. 我想用它来读取17770文件中的值,并将它们全部添加到一个字典对象的末尾。 I have a machine with 8 cores. 我有一台8核的机器。

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. 这是开始的python库参考的一些好部分。

http://docs.python.org/py3k/library/threading.html http://docs.python.org/py3k/library/threading.html

http://docs.python.org/py3k/library/_thread.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. 至于如何有效地使用线程,我建议您谷歌“ Python线程教程”或类似的东西。

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 . 尽管Python支持线程化,但是标准实现(CPython)一次执行一个线程 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.) (但是,如果您使用的是JPython或IronPython,则此限制不适用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM