Skip to main content
 首页 » 编程设计

python之如何在 Python 中并行化 I/O 绑定(bind)操作

2025年05月04日11jpfss

我正在用 tweepy 处理推文:

class StdOutListener(StreamListener): 
    def on_data(self, data): 
        process(json.loads(data)) 
        return True 
 
l = StdOutListener() 
stream = Stream(auth, l) 
stream.filter(track=utf_words) 

process 函数获取包含在推文中的 URL 内容(带有请求),使用 nltk 处理数据(我猜这会使用一些 CPU)并将结果保存到 Mongo 中。

问题是获取包含的 URL 的内容需要很长时间,因此限制了我的处理速度。我如何以 python 方式加速这件事?

请您参考如下方法:

你可以使用python的threading模块:

import threading 
 
class YourThreadSubclass(threading.Thread): 
    def __init__(self,your_args): 
        threading.Thread.__init__(self) 
        #do whatever setup you want 
     
    def run(self): 
        process_data(self.some_property) 
 
threads = [YourThreadSubclass(args) for args in Iterable] 
for t in threads: 
    t.start() 
for t in threads: 
    t.join() 
return reduce(combiner, (t.result_field for t in threads)) 

更多信息在这里:http://docs.python.org/2/library/threading.html

编辑:更直接地说,您可以在调用 on_data 时 fork 一个线程。

def on_data(self, data): 
    YourThreadSubclass(data).start() 

fork 线程将异步存储其结果。

如果您要处理大量请求,您可能还想使用线程池来管理您的线程。文档 here