[Python] Simplifying development. C++ function retaining value across calls from C++ or...

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    Having the scenario of a C++ library where a function does some calculations as it receives a container normally (not always) across multiples calls, and where the function needs to retain an intermediate result across those calls, then I'm looking for a simplified way to prepare the function to be invocated in more than 1 place in a program independently, in the case to need to call the same function in the same program/file in 2 or more different places, being separate/independent invocations. Also, at the same time the function could be called from different programs.

    #include "funcs.hpp"

    //...

    for (int i = 0; i < n; i++) {
    //...

    auto var1 = func1(vec1, a1);

    auto var2 = func1(vec2, a2); // An invocation separated from the one for var1, so
    // if the func1 intermediate result starts in 0 and in
    // the 1st iteration it gets 1 for the var1 call, then
    // that here for var2 it starts in 0 again, and in the
    // 2nd iteration continues from 1 for var1, and so on.
    }

    //...


    In this scenario, the key part is that the invocations to func1 for var1 keep the track across the calls of the retained state specifically for that instruction (or session id, or instance of func1, or the exact terminology for it), in the way that the intermediate func1 results for var1 don't get "mixed" (be independent) with the intermediate func1 results for var2. And the same for the rest of possible func1 invocation instructions either in the same program or in multiple programs.

    For now, this is being handled with thread_local static std::unordered_map<..., ...>:

    funcs.hpp

    //...

    std::vector<int> func1(const std::vector<int>& v1, int a, session_id = "__default__");

    //...


    funcs.cpp

    #include "funcs.hpp"

    //...

    std::vector<int> func1(const std::vector<int>& v1, int a, session_id) {

    thread_local static std::unordered_map<std::string, int> x;
    thread_local static std::unordered_map<std::string, std::vector<int>> y;

    std::string key_ = session_id ? session_id : "__default__";

    //thread_local static z // Before the situation to call func1 in more than 1 place.
    //thread_local static vr // Before the situation to call func1 in more than 1 place.

    int& z = x[key_];
    std::vector<int>& vr = y[key_];

    for (int i = 0; i < v1.size(); i++) {
    z += v1;

    std::cout << "z: " << z << "\n";
    }

    //...

    return vr;
    }


    The main question is, could this situation be handled in a way where there is no need to specify an additional parameter (without session_id, without the map)? because for now, that additional parameter has the only purpose to make an invocation differentiation, so the main purpose of this question is if there would be way to have an equivalent behavior of the func1 scheme without the need to handle it "manually" with the extra parameter session_id.



    More context about this question

    • The function doesn't store any information in a file the drive, so this is for the scenario only during the lifespan of the calling program.



    Main requirements


    • The Function needs to be called from multiple languages with 1 single implementation, for now mainly called from C++ programs and called from Python with a SWIG/Cython wrapper.


    • Simplified call instruction. A function design where its calls be done directly in 1 single instruction (in C++, in Python, ...) just like right above, i.e. a function design where the calls don't need extra steps like requiring an explicit instance instruction (classes) before the call, or other kind of extra step in addition to the call instruction itself.


    • Multi-threading ready. The function design needs to be prepared for a future scenario where the calling program runs in multi-thread, in the case where the threads don't share information among them (each thread having its separate/independent task).



    Update 1

    Criteria for determining when 2 or more calls won't use the same session/context

    Having 2 or more calls like the next:

    # Python calling instructions in a .py file:
    # ...
    var1 = py_func1(vec1, a1)
    var2 = py_func1(vec2, a2)


    # C++ calling instructions in a .cpp file
    # ...
    # auto var1 = func1(vec1, a1);
    # auto var2 = func1(vec2, a2);


    For 2 or more func1 identical invocations like these 2 examples with var1 and var2, the criteria for determining they won't use the same session/context is simply that each invocation will always have its own independent retained value, so the func1 invocations in var1 will always be independent from the func1 invocations in var2, and a way to visually identify this, is by just seeing the 2 invocations are in different lines in the same file or in same line but in different columns in the same file. So, for example about var1 = py_func1(vec1, a1), the idea in this question is that the subsequent invocations in var1 could automatically keep its track about the previous retained value specifically for it, without "collisions" (without mixing) the func1 retained value in var1 with the func1 retained value in var2. To control this situation, an option is to have an additional parameter session_id, but the question is mainly about a possibility to get an equivalent solution without the need to have that session_id parameter, and why? for simplicity, so in the case you need to call the function in 20 different places in the same program, then that ideally there is no need that the programmer manually keep the track about what session_id was specified here in the program line 140, or there in the program line 500, so that there is no need to hardcode a parameter like session_id.

    Continue reading...

Compartilhe esta Página