当前位置:   article > 正文

基于pytorch实现任意python任务多线程处理_pytorch 任务管理

pytorch 任务管理

不啰嗦,直接上代码!

from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader

__all__ = ["MultiProcessor", "taskCore"]


class taskCore(Dataset):
    def __init__(self, custom_list, *args, **kwargs):
        self.custom_list = custom_list
        self.args = kwargs
        self.kwargs = kwargs
        pass

    def __len__(self):
        return len(self.custom_list)

    def pro_func(self, *args, **kwargs):
        """替换for循环处理内容"""
        pass

    def custom_item(self, custom_list, idx):
        """替换从for循环中取数据"""
        return custom_list[idx]

    def __getitem__(self, idx):
        return self.pro_func(self.custom_item(self.custom_list, idx), *self.args, **self.kwargs)


def collate_fn(item):
    return 0


class MultiProcessor:
    def __init__(self, custom_list, custom_taskCore=None, *args, **kwargs):
        self.taskCore = custom_taskCore if custom_taskCore else taskCore(custom_list, *args, **kwargs)
        self.handler = DataLoader(dataset=self.taskCore, collate_fn=collate_fn, num_workers=8)

    def set_custom_func(self, pro_func, custom_item=None):
        self.taskCore.pro_func = pro_func
        if custom_item is not None:
            self.taskCore.custom_item = custom_item

    def __call__(self, *args, **kwargs):
        for item in tqdm(self.handler):
            pass


def main():
    pass


if __name__ == '__main__':
    main()
    pass

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/263729
推荐阅读
相关标签
  

闽ICP备14008679号