473,782 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delivering data to python from a c-thread

Hi,

I have a C-application that calls a Python function main(). This
function will loop forever and not return until the entire application
is about to terminate.

In a parallel C-thread, some data must be regularly delivered to the
running python application. My initial plan was to call a py function
deliver() from this thread, however I constantly run into troubles. I
keep getting a "Fatal Python error: ceval: tstate mix-up" error from
python, even when I handle the GIL with PyGILState_Ensu re().

I fail to use PyEval_AcquireL ock() prior to python call either, as the
main app would then constantly keep this lock when its running
permanently in python.

Basically I would the thread to stop the execution of the main py app,
call the message function deliver(). When the function returns from
python, resume the execution of the main pyapp.
Regards,
Svein Seldal

Nov 8 '06 #1
5 1378
Svein Seldal wrote:
Hi,

I have a C-application that calls a Python function main(). This
function will loop forever and not return until the entire application
is about to terminate.

In a parallel C-thread, some data must be regularly delivered to the
running python application. My initial plan was to call a py function
deliver() from this thread, however I constantly run into troubles. I
keep getting a "Fatal Python error: ceval: tstate mix-up" error from
python, even when I handle the GIL with PyGILState_Ensu re().

I fail to use PyEval_AcquireL ock() prior to python call either, as the
main app would then constantly keep this lock when its running
permanently in python.

Basically I would the thread to stop the execution of the main py app,
call the message function deliver(). When the function returns from
python, resume the execution of the main pyapp.
Could you have the Python code create a second Python thread and have it
call back into the C code to collect any waiting data?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 8 '06 #2
Steve Holden wrote:
Could you have the Python code create a second Python thread and have it
call back into the C code to collect any waiting data?
Well yeah, in principle. However one would need some synchronization
mechanisms anyway. The C data source is generating asynch. messages to
deliver to python and thus the py thread must be ready to wait for it.
It will add another thread in the total application (cuz' I cant remove
the extra C thread since it has other important tasks), but I'll give it
a shot at least!

Regards
Svein Seldal
Nov 8 '06 #3
Svein Seldal wrote:
Steve Holden wrote:
>Could you have the Python code create a second Python thread and have it
call back into the C code to collect any waiting data?

Well yeah, in principle. However one would need some synchronization
mechanisms anyway. The C data source is generating asynch. messages to
deliver to python and thus the py thread must be ready to wait for it.
It will add another thread in the total application (cuz' I cant remove
the extra C thread since it has other important tasks), but I'll give it
a shot at least!
OK. I was just thinking that, with Python threads, communication using
Queue.Queue is thread-safe and will handle the GIL, so that way you only
have the problem of how to synchronize your C code when it receives the
callback from the Python thread.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 8 '06 #4
Steve Holden wrote:
OK. I was just thinking that, with Python threads, communication using
Queue.Queue is thread-safe and will handle the GIL, so that way you only
have the problem of how to synchronize your C code when it receives the
callback from the Python thread.
The python internal thread-safe problem is under control. I'm using
twisted, so it already provide me with a thread-synchronization
mechanism (the reactor.callFro mThread() ).

The issue is rather the C sync-ing. I'm kind of new to multithreaded
(pthreads) programming, so I'm not quite sure what exists of
functionality and functions. So this is how I think of doing it:

Setup a mailbox or similar where the producing thread will deliver its
message. This call to the mailbox should block until the consumer thread
has received the message. My py thread will repeatedly call my
C-handler. This C handler will wait (forever) for incoming messages from
the producer. This should work, right? I mean, this could even be
implemented with flags/semaphores and let the data be transferred over
ordinary common global variables.

Francly, I think the best solution would be to be able to call py from
two independent c-treads!

Regads,
Svein Seldal
Nov 8 '06 #5
Steve Holden wrote:
OK. I was just thinking that, with Python threads, communication using
Queue.Queue is thread-safe and will handle the GIL, so that way you only
have the problem of how to synchronize your C code when it receives the
callback from the Python thread.
The python internal thread-safe problem is under control. I'm using
twisted, so it already provide me with a thread-synchronization
mechanism (the reactor.callFro mThread() ).

The issue is rather the C sync-ing. I'm kind of new to multithreaded
(pthreads) programming, so I'm not quite sure what exists of
functionality and functions. So this is how I think of doing it:

Setup a mailbox or similar where the producing thread will deliver its
message. This call to the mailbox should block until the consumer thread
has received the message. My py thread will repeatedly call my
C-handler. This C handler will wait (forever) for incoming messages from
the producer. This should work, right? I mean, this could even be
implemented with flags/semaphores and let the data be transferred over
ordinary common global variables.

Francly, I think the best solution would be to be able to call py from
two independent c-treads!

Regads,
Svein Seldal

Nov 8 '06 #6

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

Similar topics

4
3847
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a socket read after a certain delay. The logic is (I will provide a test program below): - create and connect socket
0
1013
by: John | last post by:
My WEB is served from Win2K with sp 4 as well as a 2003 server. All patches and upgrades are in place on both machines. The issue is that my WEB delivers properly, accessing SQL, delivering all information except for five pages and then the browser times out and I get an error message that the "aspnet_wp.exe stopped unexpectedly" Does any of you have an idea what is happening. I've checked my code, connections, impersonations and still...
0
1031
by: John | last post by:
My WEB is served from Win2K with sp 4 as well as a 2003 server. All patches and upgrades are in place on both machines. The issue is that my WEB delivers properly, accessing SQL, delivering all information except for five pages and then the browser times out and I get an error message that the "aspnet_wp.exe stopped unexpectedly" Does any of you have an idea what is happening. I've checked my code, connections, impersonations and still...
0
241
by: John | last post by:
My WEB is served from Win2K with sp 4 as well as a 2003 server. All patches and upgrades are in place on both machines. The issue is that my WEB delivers properly, accessing SQL, delivering all information except for five pages and then the browser times out and I get an error message that the "aspnet_wp.exe stopped unexpectedly" Does any of you have an idea what is happening. I've checked my code, connections, impersonations and still...
11
2819
by: nn0410 | last post by:
I am about to deliver my first Access application to a client and am trying to do things "right" now, so as to make future maintenance as painless as possible for the client and myself. Following the initial delivery and installation, I'll be conducting ongoing development/maintenance on my premises, and then visiting the client's office periodically to install updates. Having never done this before, I'd greatly appreciate a reality...
0
9480
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
10313
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
10080
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
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.