Docker内安装运行bt Ctpbee Store

警告
本文最后更新于 2024-10-24,文中内容可能已过时。

最近在搞量化交易, 因为backtrader的a股实盘对接, 需要去看backtrader的对接机制. 但是因为python语言不太熟, 静态跟踪代码太烧脑子, 于是我打算找个现成写好的实盘项目研究一下, 于是我选中了bt-ctpbee-store

我比较喜欢在docker内干活, 于是用了anaconda3的docker镜像作为我的开发环境

sh

# 如果太慢就挂个梯子
pip3 install git+http://github.com/phonegapx/bt-ctpbee-store

在正常环境下不会有问题, 但是docker就不同了, 会崩溃在ctpbee_api的cpp模块中, 实在没办法, 我只好用gdb调试, 因为gdb在docker内调试对权限有要求, 于是我又调整了我的docker容器到特权模式,如下

yaml

name: anaconda3
services:
    anaconda3:
        cpu_shares: 90
        command:
            - /bin/bash
            - -c
            - conda install -c conda-forge jupyterlab -y --quiet && jupyter lab --notebook-dir=/opt/notebooks --ip='*' --port=8888 --no-browser --allow-root
        container_name: anaconda3
        cap_add:
            - SYS_PTRACE
        security_opt:
            - seccomp:unconfined
        privileged: true

sh


conda activate develop_env

gdb python

(gdb) run test_script.py

# 等到崩溃的时候, 查看堆栈信息, 发现是zh_CN.GB18030字符集在docker环境中没有安装
(gdb) bt

Thread 5 "python" hit Catchpoint 1 (exception thrown), __cxxabiv1::__cxa_throw (obj=0x7fffd8001380, 
    tinfo=0x7fffe9b68220 <typeinfo for std::runtime_error>, dest=0x7fffe9a28110 <std::runtime_error::~runtime_error()>)
    at /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/libsupc++/eh_throw.cc:77
77      /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/libsupc++/eh_throw.cc: No such file or directory.
(gdb) bt
#0  __cxxabiv1::__cxa_throw (obj=0x7fffd8001380, tinfo=0x7fffe9b68220 <typeinfo for std::runtime_error>, 
    dest=0x7fffe9a28110 <std::runtime_error::~runtime_error()>)
    at /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/libsupc++/eh_throw.cc:77
#1  0x00007fffe9a0a2cf in std::__throw_runtime_error(char const*) () from /opt/conda/envs/quant/bin/../lib/libstdc++.so.6
#2  0x00007fffe9a34498 in std::locale::facet::_S_create_c_locale (__cloc=@0x7fffe5427b18: 0x0, 
    __s=__s@entry=0x7fffeb923720 "zh_CN.GB18030", __old=__old@entry=0x0) at c++locale.cc:139
#3  0x00007fffe9a26950 in std::locale::_Impl::_Impl (this=0x7fffd8000ea0, __s=0x7fffeb923720 "zh_CN.GB18030", 
    __refs=<optimized out>)
    at /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/src/c++98/localename.cc:194
#4  0x00007fffe9a274e5 in std::locale::locale (
    this=0x7fffeb92e858 <toUtf(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)::loc>, 
    __s=0x7fffeb923720 "zh_CN.GB18030")
    at /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/src/c++98/localename.cc:43
#5  0x00007fffeb9093be in toUtf(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /opt/conda/envs/quant/lib/python3.11/site-packages/ctpbee_api/ctp/vnctpmd.cpython-311-x86_64-linux-gnu.so
#6  0x00007fffeb900dbf in MdApi::processRspUserLogin(Task*) ()
   from /opt/conda/envs/quant/lib/python3.11/site-packages/ctpbee_api/ctp/vnctpmd.cpython-311-x86_64-linux-gnu.so
#7  0x00007fffeb905325 in MdApi::processTask() ()
   from /opt/conda/envs/quant/lib/python3.11/site-packages/ctpbee_api/ctp/vnctpmd.cpython-311-x86_64-linux-gnu.so
#8  0x00007fffe9a3dbf4 in std::execute_native_thread_routine (__p=0x23f2740)
    at /opt/conda/conda-bld/gcc-compiler_1654084175708/work/gcc/libstdc++-v3/src/c++11/thread.cc:82
#9  0x00007ffff7fabea7 in start_thread (arg=<optimized out>) at pthread_create.c:477

sh

# docker安装字符集管理工具
apt install locales

# 验证系统中是否存在字符集
locale -a

# 安装字符集
locale-gen zh_CN.GB18030

# 刷新字符集设置(不要选择1, 安哪个字符集就选哪个字符集, 不然太大了)
dpkg-reconfigure locales

# 验证系统中是否存在字符集(存在, 重新运行就过去了)
locale -a