473,394 Members | 1,750 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,394 software developers and data experts.

Threading a lengthy C function


Hi all,

In a GUI program I'm currently developing (wxWindows, if that
matters), I am trying to get some time-consuming tasks (triggered
by e.g. the user choosing a menu item) out of the interface
thread and into 'worker' threads of their own, in order to stop
them from locking up the GUI.
If I subclass threading.Thread, and I override run() as follows:

def run(self):
retvalue = time.sleep(50)

then everything works perfectly fine when I start up this Thread
in response to the user trigger -- the GUI unlocks and the
application windows will repaint correctly, etc.

If, however, I use the kind of run() method that I *really* need:

def run(self):
retvalue = mypackage.sleep(50)

where mypackage.sleep() is in fact a SWIG-generated wrapper
around the C library sleep() function, I have no such luck: the
entire application will *still* lock up completely until this
task is done.

My guess would be (but I'm new to Python, so I may be entirely
wrong) that this has something to do with that Global Interpreter
Lock thing I keep reading about. I can imagine that if my lengthy
task is a Python task, the interpreter will get the chance to
switch between threads, but that if the task is an external C
function, this will be executed 'atomically' with no chance for
the interpreter to release the GIL to other threads.

I'd like to know if this interpretation of what's happening is
correct (or if it isn't, then what is?), but most importantly I
was wondering if anybody could point me in the direction of a
solution or workaround to my actual problem.

Many thanks in advance,

--
Leo Breebaart <le*@lspace.org>
Jul 18 '05 #1
2 1367
Leo Breebaart <le*@lspace.org> wrote in news:bp**********@news.tudelft.nl:
I'd like to know if this interpretation of what's happening is
correct (or if it isn't, then what is?), but most importantly I
was wondering if anybody could point me in the direction of a
solution or workaround to my actual problem.

Your interpretation sounds spot on.

What you need to do is to release the GIL as the last thing that happens in
your C code before you call the lengthy function, and reclaim it as the
first thing you do when the function returns.

It may be easiest simply to wrap the library function in another C function
that does this, and then use SWIG to wrap the new function instead of the
original. e.g.

#include <python.h>
int safe_thefunction(char *arg, int arg2)
{
int res;
Py_BEGIN_ALLOW_THREADS
res = thefunction(arg, arg2);
Py_END_ALLOW_THREADS
return res;
}

and then wrap safe_thefunction instead of thefunction.

If the function you are wrapping can call back into Python then you must
also be sure to reclaim the GIL during each callback. This is easy enough
to do if the callbacks all happen on the same thread, but can be a pain if
there are asynchronous callbacks on other threads.

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2

My thanks to everyone who participated in this, ahem, thread.
Your responses have been very helpful -- this newsgroup is a
wonderful resource for the beginning Python programmer.
Duncan Booth <du****@NOSPAMrcp.co.uk> writes:
It may be easiest simply to wrap the library function in another C function
that does this, and then use SWIG to wrap the new function instead of the
original. e.g.

#include <python.h>
int safe_thefunction(char *arg, int arg2)
{
int res;
Py_BEGIN_ALLOW_THREADS
res = thefunction(arg, arg2);
Py_END_ALLOW_THREADS
return res;
}


This is more or less exactly what I ended up doing, and
everything works like a charm now.

--
Leo Breebaart <le*@lspace.org>
Jul 18 '05 #3

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

Similar topics

5
by: James Morton | last post by:
I have a Static DataSet in a class file that I am using globally between a few forms. The main form populates the dataset through a menu option which invokes ReadXML in the class file to populate...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
9
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
3
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
8
by: Patrick Smith | last post by:
Hi, I'm hoping someone here will be able to help as I've been struggling with this problem for a few days now. I'm working on an application that is creating a ProgressDialog, and then creating...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
1
by: Ajak | last post by:
Hi all, I would like to write a class (Task) with a method to do some lengthy process based on several of the class properties. The method is running on different thread. During the execution...
2
by: mikelis | last post by:
I have a main form that does a lengthy database query. Before I fire off the query, I need to pop up another form that contains a progress bar and a Close button. The progress bar updates for 30...
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
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,...
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
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,...
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...

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.