繁体   English   中英

Python在同一资源上执行不同的操作

[英]Python different operations on the same resources

我正在尝试分析具有不同任务的大型python文件。 我已经阅读并预处理了文件,现在它已经在内存中。 问题是,我所拥有的任务,它们几乎必须遍历整个记录列表。 类似于:

resourceList = [..] #list of records from the file (say, 2GB)
def taskA():
    for i in resourceList:
        #doSthA()

def taskB():
    for i in resourceList:
        #doSthB()

如果我执行taskA(),然后执行taskB(),它将两次遍历2GB的文件,而且速度非常慢。 这是taskA和taskB可以同时同时完成其工作的一种方式,这样我就不必两次执行任务了?

我读到一些涉及python线程和Queue的东西,这是唯一(正确的)方法吗? 如果是这样,如果“ resourceList”是生成器而不是列表怎么办?

谢谢!

我会使用线程来实现这一点(因为我发现这个问题更容易解释何时每个任务是一个单独的线程,并通过多处理线程化以便可以共享数据),然后将每个函数传递给一个可以迭代的队列:

import threading
from Queue import Queue

class IterableQueue(Queue): 
    _sentinel = object()

    def __iter__(self):
        return iter(self.get, self._sentinel)

    def close(self):
        self.put(self._sentinel)

def taskA(resources):
    for resource in resources:
        do_stuff()

def taskB(resources):
    for resource in resources:
        do_stuff()

def start_thread(task):
    queue = IterableQueue(maxsize=1)
    thread = threading.Thread(target=task, args=(queue, ))
    thread.start()
    return (thread, queue)

threads = [
    start_thread(taskA),
    start_thread(taskB),
]

resource_list = [...]

for resource in resource_list:
    for _, queue in threads:
        queue.put(resource)

for thread, _ in threads:
    thread.join()

暂无
暂无

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

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