473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
Jul 18 '05 #1
0 2492

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

Similar topics

7
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....
5
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...
7
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...
22
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
3
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; };
7
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...
27
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...
0
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...
21
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.