Some questions embedding Pythong in C (callback for variable assignment in the dictionary?)

Discussion in 'Python' started by jordi, Jul 16, 2003.

  1. jordi

    jordi Guest

    Hi,

    I'm starting to use Python embedded in a C program. I'm using Python
    to execute several scripts using as a variables information retrieved
    for several multithread "agents" written in C. The method is:

    1- Create a thread for each agent (I use a pool thread, every agent
    run in a different thread)
    2- Precompile a different script for each agent.
    3- Make the agent retrieve its data
    4- Convert this data to Python variables
    5- Execute a Python script.

    The steps 3 to 5 are executed again and again (there is a timer that
    launch the execution). As it's a multithreaded program my code is
    something like this:

    - In each thread initialization:

    PyEval_AcquireLock();
    interp = Py_NewInterpreter();
    PyThreadState_Swap(interp);

    globals = PyDict_New();
    PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins());
    pCode = Py_CompileString(ScriptPython, "testPython", Py_file_input
    );

    (This "ScriptPython" is different depending on the type of agent)

    - Then, every time the C "agent" retrieves its data:

    PyEval_AcquireThread(interp);
    /* Here I have to modify the PyDictionary, globals or locals */
    result = PyEval_EvalCode((PyCodeObject *)pCode, globals,
    0/*locals*/);
    PyEval_ReleaseThread(interp);

    I have some questions:

    1.- Is there any way to make the embedded Python interpret make a
    callback to the C program asking for variables when they are not
    defined in the Dictionary ? That is, implementing an "variable
    on-demand" method. I have a problem because an "agent" can have
    hundreds or thousands variables and maybe a script only uses a small
    number of them. Inserting all the variables every time can be very
    time consuming. I think Lua language can do that using "fallbacks"
    or "tag methods" but I don't know if Python has something like that.

    2.- What is the best way to use Python embedded in a C program?.
    Instead of creating a new interpreter (Py_NewInterpreter();) I tried
    using only a Python interpreter and creating a different state for
    each thread:

    interp = PyThreadState_New(PyMainThreadState->interp);

    This solution was much worse than creating different Interpreters. Is
    it normal?

    3.- Are there any optimization tricks to use in embedded Python to
    improve performance?

    4.- Have you used any other script language for embedding in a
    multithreaded program? I'm looking for something portable, fast and
    free for commercial use. I have tested Python and Lua and Lua seems
    more suitable (and much faster) for simple embedding in a
    multithreaded program.

    Best regards,

    Jordi
     
    jordi, Jul 16, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jari Aalto+usenet
    Replies:
    0
    Views:
    379
    Jari Aalto+usenet
    Aug 19, 2003
  2. Hugh Beyer
    Replies:
    2
    Views:
    315
    Hugh Beyer
    Mar 17, 2006
  3. mobil
    Replies:
    3
    Views:
    248
    Basilisk96
    May 1, 2007
  4. Tim Spens
    Replies:
    1
    Views:
    1,084
    Matimus
    Jun 27, 2008
  5. Basile STARYNKEVITCH

    embedding ruby - some questions

    Basile STARYNKEVITCH, Aug 31, 2003, in forum: Ruby
    Replies:
    0
    Views:
    124
    Basile STARYNKEVITCH
    Aug 31, 2003
Loading...

Share This Page