Jump to content

How to pass a callback function from Pascal as a parameter to a Python function?

Recommended Posts

I have tried several approaches to pass a Delphi callback function to be used by Python, but the functions are not being called.

procedure CallbackFunc(args: Variant);
// ...

callback:=PythonEngine1.PyCFunction_NewEx(@CallbackFunc, nil,nil);

//codelist := VarPythonCreate(['SZ']);
codelist := PythonEngine1.PyList_New(1);
PythonEngine1.PyList_SetItem(codelist, 0, PythonEngine1.PyUnicode_FromString('110072.SH'));

argsTuple := PythonEngine1.PyTuple_New(2);
PythonEngine1.PyTuple_SetItem(argsTuple, 0, codelist);
PythonEngine1.PyTuple_SetItem(argsTuple, 1, callback);
ss :=GetPythonEngine.EvalPyFunction(ExtractPythonObjectFrom( client.subscribe_whole_quote ), argsTuple); 


in the code subscribe_whole_quote is a python function have 2 parms, 1st is a list, and 2d is the callback function, when I called the subscribe_whole_quote it returned success, but never call the callback function.


int the c# I can only pass the callback like a delegate like:

public delegate void onSubDelegate(dynamic datas);
 public void onWholeSub(dynamic jdatas)

var onWholeSubdel = new onSubDelegate(onWholeSub);

  List<string> stocklist = new List<string>(){"SH"};
  var ret = client.subscribe_whole_quote(stocklist, onWholeSubdel);
  return ret;
catch (Exception ex)


it is works fine.

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now