Python process TypeError: cannot pickle '_thread.lock' object
Ran into a wall here a little. Basically I've got a python program which can launch multiple bots from within, and I'm trying to launch each bot as a separate process. So i've created a class that holds all the instances so that I can later stop them when needed.
In that class I've got a method that launches the processes.
def start_new_bot(self, bot_id, commission):
# Other things calculations...
trader = BotTrader(
bot=db_bot,
commission=commission,
symbol_filters=symbol_filters,
api_key=api_key,
api_secret=api_sec,
api_pass=api_pass,
)
process = Process(target=trader.start_trading)
self.instances.append(process)
process.start()
process.join()
And my BotTrader class looks like this:
class BotTrader():
def __init__(
self,
bot,
commission,
symbol_filters,
api_key,
api_secret,
api_pass="",
):
# Bot options
self.accumulate_wallet_profit = bot["accumulate_wallet_profit"]
for filter in symbol_filters:
if filter["filterType"] == "LOT_SIZE":
self.LOT_SIZE = filter["stepSize"].find("1") - 1
elif filter["filterType"] == "PRICE_FILTER":
self.STEP_SIZE = filter["tickSize"].find("1") - 1
self.trade_manager = TradeManager(
bot_obj=self.db_bot,
step_size=self.STEP_SIZE,
lot_size=self.LOT_SIZE,
commission=commission,
logger=self.logger,
)
self.orders = Orders(
exchange=bot["exchange"],
api_key=api_key,
api_secret=api_secret,
api_pass=api_pass,
step_size=self.STEP_SIZE,
lot_size=self.LOT_SIZE,
commission=commission,
)
# order book
self.orderbook_manager = OrderBookManager(
bot_obj=self.db_bot,
trade_symbol=self.TRADE_SYMBOL,
logger=self.logger,
)
def start_trading(self):
try:
websocket.enableTrace(False)
self.orderbook_manager.initialize()
socket_url = (
"wss://stream.binance.com:9443/ws/"
+ self.TRADE_SYMBOL.lower()
+ "@kline_"
+ self.INTERVAL
)
self.WEBSOCKET = websocket.WebSocketApp(
url=socket_url,
on_message=self.on_websocket_message,
on_error=self.on_error,
)
self.WEBSOCKET.run_forever()
except SysCallError as e:
sentry_sdk.capture_exception(e)
When process.start() is called i get the error TypeError: cannot pickle '_thread.lock' object Not sure where to start fixing this.
Before I used Threading. And it worked, BotTrader was a daemon thread
trader = BotTrader(
bot=db_bot,
commission=commission,
symbol_filters=symbol_filters,
api_key=api_key,
api_secret=api_sec,
api_pass=api_pass,
)
trader.start()
self.instances.append(trader)
class BotTrader(threading.Thread):
super(BotTrader, self).__init__()
self.daemon = True
def run(self):
try:
if self.bot_running:
self.start_trading()
except Exception as e:
sentry_sdk.capture_exception(e)
self.print_message(
f"Encountered fatal error in run method:
I would sidestep the problem of not being able to send a BotTrader
instance to another process by simply creating it in the process in which it will be used:
@staticmethod
def bot_proc_target(bot_id, commission):
trader = BotTrader(
bot=db_bot,
commission=commission,
symbol_filters=symbol_filters,
api_key=api_key,
api_secret=api_sec,
api_pass=api_pass,
)
trader.start_trading()
def start_new_bot(self, bot_id, commission):
# Other things calculations...
process = Process(target=self.bot_proc_target, args=(bot_id, commission))
self.instances.append(process)
process.start()
process.join()