Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:42:39+00:00 2026-05-26T00:42:39+00:00

I want to pass callback from my python code to c++ I want my

  • 0

I want to pass callback from my python code to c++

I want my code look something like this:
In C++ :

typedef void (*MyCallback_t) (CallbackInfo);

class MyClass
{...
   void setcallback(MyCallback_t cb);
 ...
}

And to use it in python :

import mylib

def myCallback(mylib_CallbackInfo):
...

t = mylib.MyClass()
t.setcallback(myCallback)

I saw some topics near my problem but couldn’t solve it

For example here :
Realtime processing and callbacks with Python and C++ there is advice to use boost::python and warning about GLI but no examples.
And here

How to call a python function from a foreign language thread (C++) there is no full description with python code part and with “BOOST_PYTHON_MODULE” part

I also found link to use py_boost_function.hpp for example in Boost python howto but it didn’t compile and actualy I couldn’t understand how to use it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:42:40+00:00Added an answer on May 26, 2026 at 12:42 am

    Ok, I’m still trying to figure this out too, but here’s whats working for me so far:

    #this is the variable that will hold a reference to the python function
    PyObject *py_callback;
    
    #the following function will invoked from python to populate the call back reference
    PyObject *set_py_callback(PyObject *callable)
    {
        py_callback = callable;       /* Remember new callback */
        return Py_None;
    }
    ...
    #Initialize and acquire the global interpreter lock
    PyEval_InitThreads();
    
    #Ensure that the current thread is ready to call the Python C API 
    PyGILState_STATE state = PyGILState_Ensure();
    
    #invoke the python function
    boost::python::call<void>(py_callback);
    
    #release the global interpreter lock so other threads can resume execution
    PyGILState_Release(state);
    

    The python function is invoked from C++, and executes as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass an enum value as command parameter in WPF, using something
I want to pass in the tType of a class to a function, and
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to implement a class in c++ that has a callback. So I
I would like to be able to pass a parameter in a callback, but
I want to pass the address of a sub as a callback to another
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void
i'm currently trying to set up something like this: a server side windows wcf
I want to code the apply_method function: class A: def calc(self, num): return num
I have this code: protected static string MakeGetRequest(string url, Action<IAsyncResult> callback) { var request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.