Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python线程:第一步_Python_Multithreading - Fatal编程技术网

python线程:第一步

python线程:第一步,python,multithreading,Python,Multithreading,我是python线程的新手,我写的第一个任务是: from twitterHandler import Twitter_User from text_analyzer import text_analyzer import threading if __name__=='__main__': usersIDS = {'user1':24503301,'user2':7375922343546478338,'user3':2144265434,'user4':50090727}

我是python线程的新手,我写的第一个任务是:

from twitterHandler import Twitter_User
from text_analyzer import text_analyzer
import threading

if __name__=='__main__':
    usersIDS = {'user1':24503301,'user2':7375922343546478338,'user3':2144265434,'user4':50090727}
    threads = {}

    def get_data(user_id):
    '''get the most common words used by twitter user un his text tweet and save it in a file object'''
        ...

    for user_name, user_id in usersIDS.items():
        t = threading.Thread(target=get_data,args=(user_id,))
        threads[user_name] = t
        print('Starting to get data for: {}'.format(user_name)
        t.start()


    for name,t in threads.items():
        t.join()
        print('Process for {} Stopped'.format(name))
代码可以工作,但我想知道这是线程的典型用例,还是我可以简单地做类似的事情

for user_id in usersIDS.values():
    get_data(user_id)
    ...

换句话说,多线程是解决我的问题的正确选择吗?非常感谢

是的,在我看来,这是一种很好的方法,而且相当普遍。在以下情况下使用螺纹:

  • 异步操作—当某个进程不依赖于另一个进程的输出时
  • 可以并行化的过程,比如对图像的不同部分进行过滤
  • 在后台运行的操作

我认为你的病例可以分为第一组和第二组。每个人-对象分析不依赖于其他人分析的输出。然而,如果你有大量的用户和文字数据,它可能会给你的计算机带来问题,因为它必须管理每个线程的创建和销毁。无论如何,是否使用线程是个人的决定。

这实际上取决于
get\u data()
中到底发生了什么。如果它需要一段时间并且是I/O绑定的,那么使用这样的线程可能是一个好方法。顺便说一下,您可以通过在userIDS.items()中对user\u name、user\u id执行
来简化第一个循环。您在第一个循环中的右边。get_data函数只是使用tweepy库和tweet api获取一些数据并将其写入文件,我无法编写整个函数,因为我无法发布帖子(大量代码和糟糕的文本),所以它对tweet api执行HTTP请求?如果是这样的话,使用线程可以加快速度。您在这里所做的几乎是复制线程池的行为。如果您运行的是python>=3.5,那么可以使用