[C++-sig] Re: Boost.Python and threads

Lijun Qin qinlj at solidshare.com
Sat Oct 4 03:57:18 CEST 2003


Hi,

If you want the unlock-lock operation be done automatically, you can use
this new 'invoke.hpp' file. I known this is not a complete solution, but you
can use it as a start.
When I initially post this message, I'm using pythoncom from win32all
package, in this module, it'll lock the thread when enter into the COM
method, so I need to unlock it first in every COM method call, the right
place is in the invoke function.
But I have found that this unlock-lock-unlock-lock sequence becomes more and
more complicated when the COM method callbacks into the caller, and this can
happen in many places because a simple AddRef() or Release() call can cause
python code executed, and AddRef()/Release() is almost anywhere in COM code,
you can image the code becomes really ugly.
Actually, in the STA (Single Thread Apartment) mode, which is common, there
is only one thread here, so no python thread lock/unlock is needed, so I
decided to implement my own COM-python bridge, and currently it's already
working in the STA mode, my existing codes have migrated into the new bridge
successfully. And this makes the patch to the invoke function unnecessary.

In the MTA(multi thread apartment) mode, I think a good solution will be
using the Channel-Hook, basically this is a point where your code will be
called when COM runtimes do RPC marshal-unmarshal, I think this is a good
place to unlock-lock python core. I have not implemented this because
currently all of my existing python object live in the a single STA, no
thread-switching is needed yet.
If somebody is interested in my python-COM bridge and willing to add MTA
support, email me and I can give you the sources, and when this is done,
I'll publish it in some places (maybe sourceforge) to share with others.

Regards
Lijun Qin
http://www.solidshare.com

----- Original Message ----- 
From: "David Abrahams" <dave at boost-consulting.com>
To: <c++-sig at python.org>
Sent: Saturday, October 04, 2003 6:28 AM
Subject: [C++-sig] Re: Boost.Python and threads


> Pierre Barbier de Reuille <pierre.barbier at cirad.fr> writes:
>
> > Hello,
> >
> > I've followed the thread about boost.python multithreading between
> > Lijun Qin and David Abrahams and I wondered where was the
> > implementation of this ? If there is no implementation, what is the
> > best way to safely unblock the python threads ? Or at least where
> > can I find information about this ?
>
> Sorry it took so long to reply Pierre; I was really hoping someone
> else (like Lijun) would answer.
>
> Anyway, I don't have an implementation of multithreading support, at
> least not yet.
>
> THe best way to safely unblock threads that I know of is to write a
> thin wrapper around your function which uses Py_BEGIN_ALLOW_THREADS
> and Py_END_ALLOW_THREADS around a call to the real function.
>
> HTH,
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: invoke.hpp.new
Type: application/octet-stream
Size: 4821 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20031004/f632c631/attachment.obj>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: thread_lock.hpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20031004/f632c631/attachment.txt>


More information about the Cplusplus-sig mailing list