dkluffy / Gamescripts

只是一个练手小脚本,别搞复杂了
1 stars 0 forks source link

AttributeError: Can't pickle local object 'shift_coords.<locals>.shifter' #1

Open dkluffy opened 4 years ago

dkluffy commented 4 years ago

多进程下的报错,多线程模式没有

def shift_coords(func):
    def shifter(*args):
        co,st = func(*args)
          co = np.array(co)
          co_dalt = np.random.randint(0,20,co.shape)
          co = co + co_dalt
        return co,st
    return shifter

@shift_coords
def matcher_comm(input_img,targets):
    coords = []
    status = []
    for t in targets.keys():
        pts = match_gray(input_img,targets[t])
        if len(pts)>0:
            coords+=pts
            status+=([t]*len(pts))
    return coords,status
I:\Code_Public\Gamescripts>I:/Anaconda3/Scripts/activate

(base) I:\Code_Public\Gamescripts>conda activate base

(base) I:\Code_Public\Gamescripts>I:/Anaconda3/python.exe i:/Code_Public/Gamescripts/main.py
Warning: some status, don't have valide image
Traceback (most recent call last):
  File "i:/Code_Public/Gamescripts/main.py", line 60, in <module>       
    p_list.append(comm_task_on_pc(["test01"],10))
  File "i:/Code_Public/Gamescripts/main.py", line 42, in comm_task_on_pc
    p0.start()
  File "I:\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "I:\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "I:\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "I:\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "I:\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'shift_coords.<locals>.shifter'

(base) I:\Code_Public\Gamescripts>Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "I:\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "I:\Anaconda3\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
dkluffy commented 4 years ago

更改之后,不用numpy

def shift_coords(func):
    def shifter(*args):
        co,st = func(*args)
        # if ON_DEBUG:
        #     return co,st
        #co = np.array(co)
        #co_dalt = np.random.randint(0,20,co.shape)
        for i in range(len(co)):
            co[i] = (co[i][0]+random.randint(1,20),co[i][1]+random.randint(1,20))
        #co = co + co_dalt
        return co,st
    return shifter

@shift_coords
def matcher_comm(input_img,targets):
    coords = []
    status = []
    for t in targets.keys():
        pts = match_gray(input_img,targets[t])
        if len(pts)>0:
            coords+=pts
            status+=([t]*len(pts))
    return coords,status
Warning: some status, don't have valide image
Traceback (most recent call last):
  File "i:/Code_Public/Gamescripts/main.py", line 62, in <module>
    p_list.append(comm_task_on_pc(["test01"],10))
  File "i:/Code_Public/Gamescripts/main.py", line 43, in comm_task_on_pc
    p0.start()
  File "I:\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "I:\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "I:\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "I:\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "I:\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'shift_coords.<locals>.shifter'

(base) I:\Code_Public\Gamescripts>Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "I:\Anaconda3\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
  File "I:\Anaconda3\lib\multiprocessing\reduction.py", line 82, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] 参数错误。
dkluffy commented 4 years ago

去掉这个 装饰函数@shift_coords 就不会有这个错误~~~~