473,406 Members | 2,387 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,406 software developers and data experts.

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_Ensure().

I fail to use PyEval_AcquireLock() 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 1369
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_Ensure().

I fail to use PyEval_AcquireLock() 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.callFromThread() ).

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.callFromThread() ).

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
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...
0
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...
0
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...
0
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...
11
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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...
0
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...
0
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,...
0
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...

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.