473,749 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi Threading embedded python

Hello,

I am embedding a python script in a C++ application. The script can be
called simultaneously from multiple threads.

What is the correct way to implement this situation:

1) Have unique python interpreter instantiations ( Py_Initialize() ) for
each thread.

2) Have one python interpreter, and implement a lock on it so it can't
be called simultaneously by multiple threads?

Thanks
Amit
Jul 19 '05 #1
1 3716
Well, depends on what you want to achieve :)

First, I don't think you can call Py_Initialize on many threads. You
have special function to initialise different interpreters on per-thread
basis.

However, the main problem is: do you want to share data across your
threads ? If the answer is 'no' then use one interpreter by thread, this
is, by far, the simplest solution (and the most efficient ?).

But if you *do* need to share data, then you are in big trouble man :)
As far as I tried it, multi-threading and Python don't go along very
well ! At least, not with system-threads. Basically, to run some Python
code, you have to hold the GIL (Global Interpreter Lock) and you cannot
*test* it, you have to try holding it, so be prepare to block your
threads that would want to access Python !

What I would recommend (and that's what I do in my own software) is to
use a single thread in which your Python interpreter is running. Then,
use a message system in C++ to send commands and get them evaluated and
sent back (if needed). By doing so, you'll avoid a lot of problems, and
you won't loose significantly performances (or if you care about that,
then Python is definitly not the language you need) nor parrallelism
(you wouldn't be able to run many Python threads at the same time anyways).

Well, I hope that help,

Pierre

amit a écrit :
Hello,

I am embedding a python script in a C++ application. The script can be
called simultaneously from multiple threads.

What is the correct way to implement this situation:

1) Have unique python interpreter instantiations ( Py_Initialize() ) for
each thread.

2) Have one python interpreter, and implement a lock on it so it can't
be called simultaneously by multiple threads?

Thanks
Amit

Jul 19 '05 #2

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

Similar topics

7
4498
by: Wenning Qiu | last post by:
I am researching issues related to emdedding Python in C++ for a project. My project will be running on an SMP box and requires scalability. However, my test shows that Python threading has very poor performance in terms of scaling. In fact it doesn't scale at all. I wrote a simple test program to complete given number of iterations of a simple loop. The total number of iterations can be divided evenly among a number of threads. My...
5
2398
by: Garry Hodgson | last post by:
a colleague of mine has seen an odd problem in some code of ours. we initially noticed it on webware, but in distilling a test case it seems to be strictly a python issue. in the real system, it manifests as webware just locking up, for no apparent reason, until we kill it. we've also had the python interpreter running webware die on occasion. it works fine on our desktop linux and windows machines, but fails on the production hardware. ...
1
5879
by: Gernot Hillier | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! I'm the developer of a Linux ISDN application which uses embedded Python for controlling the communication. It starts several threads (i.e. one for each incoming call and for outgoing faxes) which run Python scripts in embedded Python interpreters which in turn do the real communication stuff. Any incoming data or confirmations of done jobs are sent via Email.
37
4895
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours, Pujo
1
2094
by: Carl Waldbieser | last post by:
I have been considering using Python and the Reportlab library for generating PDF reports for the back-end of a web based application. The application runs most of its background tasks on a dedicated server that is Windows based (Win2K or Win2k3). The program that launches the tasks requires a COM-based interface, so I wrote a Python COM server using Mark Hammond's PythonCom libraries and import and run the reporlab modules from there. ...
1
1705
by: freesteel | last post by:
I am trying to run a python programme embedded from C++. I want to run the same python code concurrently in several threads. I read the manual on embedding, especially chapter 8, and searched for relevant info on google all afternoon, but I can't get this to work. What am I doing wrong? I use python2.4 and vc++7 (.net). The first thread seems to work okay, the 2nd thread crashes, but the exception information is not very useful: (An...
14
1748
by: danfan1981 | last post by:
Hi, I am using Python Thread library for my parallel processing course project. I am doing matrix convolution on a multi-processor machine running Solaris. I just found out that no speed-up is obtained with threading. It is probably because of something called GIL in Python. How can I get around that GIL and get speed-up? Thanks in advance. Daniel
14
3454
by: Akihiro KAYAMA | last post by:
Hi all. I found cooperative multi-threading(only one thread runs at once, explicit thread switching) is useful for writing some simulators. With it, I'm able to be free from annoying mutual exclusion, and make results deterministic. For this purpose, and inspired by Ruby(1.9) fiber, I wrote my own version of fiber in Python.
1
5690
by: Pradip | last post by:
Hello every body. I am new to this forum and also in Python. Read many things about multi threading in python. But still having problem. I am using Django Framework with Python having PostgreSQL as backend database with Linux OS. My applications are long running. I am using threading. The problem I am facing is that the connections that are being created for database(postgres) update are not getting closed even though my threads had...
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.