websocket-clientを用いて、マルチプロセスを使用した際に、「Can't pickle local object」というエラーが表示されてしまいます。
回避策を教えて下さい。
実際のコードは以下のとおりです。
python
1import json 2import websocket 3try: 4 import thread 5except ImportError: 6 import _thread as thread 7import threading 8import time 9from multiprocessing import Process 10 11class Test1(): 12 13 def __init__(self, host_addr): 14 self.host_addr = host_addr 15 self.ws = websocket.WebSocketApp(host_addr, 16 on_message=lambda ws, msg: self.on_message(ws, msg), 17 on_error=lambda ws, msg: self.on_error(ws, msg), 18 on_close=lambda ws: self.on_close(ws)) 19 self.ws.on_open = lambda ws: self.on_open(ws) 20 websocketThread = threading.Thread(target=self.run_forever, args=()) 21 websocketThread.start() 22 self.list=[] 23 24 def processing(self): 25 time.sleep(10) 26 print('a') 27 28 def on_message(self, ws, message): 29 jsonMessage = json.loads(message) 30 print(jsonMessage) 31 32 self.list.append(Process(target=self.processing).start()) 33 34 def on_error(self, ws, error): 35 print(error) 36 37 def on_close(self, ws): 38 print("### closed ###") 39 40 def on_open(self, ws): 41 thread.start_new_thread(self.run, ()) 42 43 def run(self, *args): 44 time.sleep(0.1) 45 input_data = '{ "jsonrpc": "2.0", "id": 1, "method": "logsSubscribe", "params": [{ "mentions": [ "MEisE1HzehtrDpAAT8PnLHjpSSkRYakotTuJRPjTpo8" ]} ,{ "commitment": "processed" } ]}' 46 self.ws.send(input_data) 47 48 def run_forever(self): 49 while True: 50 try: 51 self.ws.run_forever() 52 53 except Exception as e: 54 print(e) 55 time.sleep(3) 56 57HOST_ADDR = "wss://solana.genesysgo.net" 58ws_client = Test1(HOST_ADDR) 59
あなたの回答
tips
プレビュー