472,787 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

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


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:

interp = Py_NewInterpreter();

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:

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

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,

Jul 18 '05 #1
0 2434

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
by: dasif | last post by:
We are looking for several contractors with very strong C++ and Pythong development skills. Any experience working for bank/brokerage/stock exchange is a big PLUS. Please email resume to...
by: Steve Menard | last post by:
Here is my problem. I have this library thats hosts another language within python, and allows that language to call back INTO python. All is good as long as the other languages calls back on...
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
by: fctk | last post by:
hello, i have some questions. 1) do constant expressions include string literals? for example, is "hello, world" a constant expression? 2) int i = 0; is the equal sign the assignment...
by: Fozzie | last post by:
Hi, I have a problem which is quite circular, and hopefully either someone has encountered something similar or has a reason why this will not work. We have a COM library providing...
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: Mushico | last post by:
How to calculate date of retirement from date of birth
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.