環境
win10
コマンドプロントから実行
import numpy as np import time import torch.multiprocessing as mp import torch.nn as nn class main(nn.Module): def __init__(self): super(main, self).__init__() self.flan=True def process1(main): print(main.flan,"pro") main.flan=False if __name__ == '__main__': main=main() main.share_memory() process1 = mp.Process(target=process1, args=(main,)) process1.start() process1.join() print(main.flan,"ok")
Traceback (most recent call last): File "shead_testR.py", line 23, in <module> process1.start() File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\process.py", line 112, in start self._popen = self._Popen(self) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__ reduction.dump(process_obj, to_child) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) _pickle.PicklingError: Can't pickle <function process1 at 0x0000017D0E3FF438>: it's not the same object as __main__.process1 Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\spawn.py", line 99, in spawn_main new_handle = reduction.steal_handle(parent_pid, pipe_handle) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\reduction.py", line 87, in steal_handle _winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE) PermissionError: [WinError 5] アクセスが拒否されました。
.share_memory(を試していたのですが
[WinError 5] アクセスが拒否されました と出てしまい
[WinError 5] アクセスが拒否されました。について調べても出てくるのは.exe関連ばかりで
どうすればいいかわかりません
一応動いたやつ(.share_memoryは使ってない)
import numpy as np import torch from torch.multiprocessing import Process, Queue,Pipe def f(b): print(b.recv()) b.send([torch.tensor([1,2,3,4,5]),np.array([1,2,3,4,5,5,7,8])]) if __name__ == '__main__': a, b = Pipe() a.send([1, 'hello']) a.send([2, 'hello']) a.send([3, 'hello', None]) p = Process(target=f, args=(b,)) p.start() he,num=a.recv() print(he,num,"my") # prints "[42, None, 'hello']" print(num.shape,he.size()) p.join()
[1, 'hello'] tensor([1, 2, 3, 4, 5]) [1 2 3 4 5 5 7 8] my (8,) torch.Size([5]) [1] [2]
追記
いくつか修正を加えましたが 新たなエラーになりました
パラメーターが間違っています。
となっていますが どこが間違えてるのかわかりませんprocess1.start()に入れる
変数はなかったはずです
import numpy as np import time import torch.multiprocessing as mp import torch.nn as nn class main_(nn.Module): def __init__(self): super(main_, self).__init__() self.flan=True def process1(main): print(main.flan,"pro") main.flan=False if __name__ == '__main__': main=main_() main.share_memory() process1 = mp.Process(target=process1, args=(main,)) process1.start() process1.join() print(main.flan,"ok")
Traceback (most recent call last): File "shead_testR.py", line 23, in <module> process1.start() File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\process.py", line 112, in start self._popen = self._Popen(self) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__ reduction.dump(process_obj, to_child) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) _pickle.PicklingError: Can't pickle <function process1 at 0x000002570CDCF438>: it's not the same object as __main__.process1 (testpyflan) C:\Users\PC_User\Desktop\AGI>Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\spawn.py", line 99, in spawn_main new_handle = reduction.steal_handle(parent_pid, pipe_handle) File "C:\Users\PC_User\Anaconda3\envs\testpyflan\lib\multiprocessing\reduction.py", line 82, in steal_handle _winapi.PROCESS_DUP_HANDLE, False, source_pid) OSError: [WinError 87] パラメーターが間違っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/03 00:34
2021/03/03 00:35