RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1038896
Accepted
Jens
Jens
Asked:2020-10-26 21:15:57 +0000 UTC2020-10-26 21:15:57 +0000 UTC 2020-10-26 21:15:57 +0000 UTC

通过 Boost.Python 并行执行 Python 函数

  • 772

数据部分:

static Market market;

// boost::asio::thread_pool thread_pool;

static std::unordered_map < std::string, std::unordered_map < std::string,
    std::pair < Market::path_t, std::string > > > data; // future

Python设置:

static Python_Initializer python_initializer;

static boost::python::object global = boost::python::import("__main__").attr("__dict__");

boost::python::exec("from analysis.src.main import run", global, global);

static boost::python::object module = global["run"];

在Python_Initializer被称为Py_Initialize。static这里和数据部分需要避免重新构造对象,因为 此代码放置在从外部循环调用的库的 DLL 函数中。该函数run是线程安全的。

主要执行:

for (const auto & asset : market.assets())
{
    for (const auto & scale : market.scales())
    {
        auto file = market.get(asset, scale, 250);

        //std::packaged_task < std::string() > task([asset, scale, file, &module]()
        //{
        //    return boost::python::extract < std::string > (
        //      module(asset.c_str(), scale.c_str(), file.string().c_str()));
        //});

        //data[asset][scale] = std::make_pair(std::move(file),
        //  boost::asio::post(thread_pool, std::move(task)));

        std::string message = boost::python::extract < std::string >(
            module(asset.c_str(), scale.c_str(), file.string().c_str()));

        data[asset][scale] = std::make_pair(std::move(file), std::move(message));
    }
}

在当前的串行执行中,一切正常。但是,如果添加thread_pool,将后者包裹在std::string中data,std::future并将主执行中的代码替换为当前注释掉的代码,则会发生错误。它的内容没有说什么具体的,错误是在Boost.Python级别的,如果你跟踪它,那么在设置某种回调函数时读/写指针有问题。

问题:如何配置Boost.Python,让Boost的并行执行机制和标准库可以并行执行一些线程安全的Python函数?

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    стас
    2020-11-02T18:08:44Z2020-11-02T18:08:44Z

    一般的想法是为 python 解释器使用第二个线程。

    #include <Python.h>
    
    #include <boost/python/exec.hpp>
    #include <iostream>
    #include <thread>
    #include <sys/wait.h>
    
    #include <boost/python/extract.hpp>
    #include <boost/python/import.hpp>
    #include <boost/python/object.hpp>
    
    void python (std::string fork, int m, int n) {
      const char *prog = "def ack(m, n):\n"
                         "  if m == 0:\n"
                         "    return n + 1\n"
                         "  elif n == 0:\n"
                         "    return ack(m - 1, 1)\n"
                         "  else:\n"
                         "    return ack(m - 1, ack(m, n - 1))";
    
      Py_Initialize();
      try {
        std::cout << fork << std::endl;
    
        boost::python::object mainModule = boost::python::import("__main__");
        boost::python::object mainNamespace = mainModule.attr("__dict__");
    
        std::stringstream commandstream;
        commandstream << "ack(" << m << "," << n << ")";
        std::string command = commandstream.str();
        boost::python::exec(prog, mainNamespace, mainNamespace);
        int val = boost::python::extract<int>(boost::python::eval(command.c_str(), mainNamespace, mainNamespace));
        std::cout << fork << " result: " << val << std::endl;
      } catch (boost::python::error_already_set const &e) {
        PyErr_Print();
      }
    }
    
    int main (int argc, char const *argv[]) {
      pid_t pid = fork();
      if (pid == 0) {
        python("f1", 3, 4);
      } else if (pid > 0) {
        python("f2", 3, 3);
    
        int status;
        waitpid(pid, &status, 0);
      } else {
        std::cout << "Fork failed." << std::endl;
      }
    
      return 0;
    }
    

    相关链接::

    • https://stackoverflow.com/q/18213159
    • https://stackoverflow.com/q/45489225
    • https://stackoverflow.com/q/41240610
    • https://stackoverflow.com/q/28968123
    • 1

相关问题

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5