import threading
from basesite.backend.strategy.websocket_break_double_position_strategy import BreakStrategy
from basesite.utils.okexutils.OkexWebsocket import run_websocket
from basesite.utils.okexutils.okex import OkEx
counter_lock = threading.Lock() # 锁
class MyThread(threading.Thread):
def __init__(self, ok, break_strategy):
super(MyThread, self).__init__()
self.ok = ok
self.break_strategy = break_strategy
def run(self): # 定义每个线程要运行的函数
# counter_lock.acquire()
run_websocket(self.ok, self.break_strategy)
# counter_lock.release()
if __name__ == '__main__':
symbol_list = ["btc_usd", "ltc_usd", "etc_usd", "eth_usd", "xrp_usd", "eos_usd", "bch_usd"]
contract_type_list = ["this_week", "next_week", "quarter"]
ktype = '5min'
loss_point = -0.005
profit_dict = {
"0.015": 1,
"0.01": 0.7,
"0.005": 0.2
}
buy_amount = 3
strategy = 'sar_break'
thread_list = []
for symbol in symbol_list:
for contract_type in contract_type_list:
ok = OkEx()
ok.symbol = symbol
ok.contract_type = contract_type
ok.ktype = ktype
ok.loss_point = loss_point
ok.profit_dict = profit_dict
ok.buy_amount = buy_amount
ok.strategy = strategy
break_strategy = BreakStrategy()
thread_list.append(MyThread(ok, break_strategy))
for my_thread in thread_list:
my_thread.start()
# join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,一直等待其他的子线程执行结束之后,主线程在终止
for my_thread in thread_list:
my_thread.join()
Python 多线程实际应用
最新推荐文章于 2023-05-27 11:48:45 发布