Python 多线程实际应用

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值