python-3.x – 多处理队列子类问题
发布时间:2020-05-23 02:14:39 所属栏目:Python 来源:互联网
导读:我想将multiprocessing.Queue子类化,以实现抓取队列块的进程.唯一的问题是,我得到一个奇怪的TypeError? #!/usr/bin/env python#whaaaaa!?from multiprocessing import Queueclass BufferQueue(Queue): A thread/process safe queue for
|
我想将multiprocessing.Queue子类化,以实现抓取队列块的进程.唯一的问题是,我得到一个奇怪的TypeError? #!/usr/bin/env python
#whaaaaa!?
from multiprocessing import Queue
class BufferQueue(Queue):
'''A thread/process safe queue for append/popleft operations with the import
buffer.'''
def __init__(self,**kwargs):
super(BufferQueue,self).__init__(**kwargs)
def consume(self,lim):
'''Consume up to but no more than lim elements and return them in a new
list,cleaning up the buffer.
@params
lim -- the maximum (limit) to consume from the list. If less items
exist in the list then that's fine too.
'''
lim = len(queue) if len(queue) < lim else lim
return [self.popleft() for i in range(lim)]
测试这个(我将其拆分,以便我不会拉其他任何东西) | => ./tests/wtf_queue.py
Traceback (most recent call last):
File "./tests/wtf_queue.py",line 10,in <module>
class BufferQueue(Queue):
TypeError: method expected 2 arguments,got 3
编辑/更新: 解决方法multiprocessing.Queue是一个创建队列的方法,因此您应该将其用作函数my_queue = Queue().>>> from multiprocessing import Queue >>> type(Queue) <class 'method'> 正如您所看到的,不是“类型”,您将使用它来进行子类化. 如果要实现自己的队列,可以查看queue.Queue 编辑: 如果要从多处理子类化队列,请改用multiprocessing.queues.Queue,这是multiprocessing.Queue()返回的对象的类型. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 将深层嵌套的json从facebook转换为python中的dataframe
- python – 使用Panda read_csv列出索引超出范围
- python – 将不规则的列表字典转换为pandas数据帧
- Python通过命令开启http.server服务器的方法
- python – 我怎么能实现像np.where这样的东西([‘value1′,
- python批量导出导入MySQL用户的方法
- 如何统一Python Pyramid视图以处理Ajax / html表单POST
- python通过MySQLdb模块连接查询mysql数据
- python线程池的实现实例
- python使用tensorflow保存、加载和使用模型的方法
