Marcelo Matus | 6 Dec 2005 20:43
Picon

Swig/Python thread support

Thread support was added to swig/python, based in the Joseph's proposal.

Please test your interfaces using

     swig -threads -python ....

and send us a mail if you find a problem, or have suggestions, comments, 
etc.

Ah, you will need python 2.3 or later, see CHANGES.current for  more 
details.

If you need thread support for older python versions, and know how to 
safely use
threads with them, please check the  pythreads.swg file and send  us an 
implementation
patch.

Marcelo

-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
Marcelo Matus | 7 Dec 2005 08:51
Picon

Re: [Swig-user] Swig/Python thread support

More info/docs:

  CHANGES.current
  Lib/python/pythreads.swg

the original proposal:

  
http://sourceforge.net/tracker/index.php?func=detail&aid=1364704&group_id=1645&atid=101645

but mainly preserved in spirit.

Marcelo

Robert Jones wrote:

> Where can I find Joseph's proposal and/or any documentation?
>
> Rob.
>
> On 06/12/05, *Marcelo Matus* <mmatus <at> acms.arizona.edu 
> <mailto:mmatus <at> acms.arizona.edu>> wrote:
>
>     Thread support was added to swig/python, based in the Joseph's
>     proposal.
>
>     Please test your interfaces using
>
>          swig -threads -python ....
>
(Continue reading)

Ames Andreas | 7 Dec 2005 12:49

RE: Swig/Python thread support

> -----Original Message-----
> From: swig-devel-admin <at> lists.sourceforge.net 
> [mailto:swig-devel-admin <at> lists.sourceforge.net] On Behalf Of 
> Marcelo Matus
> Sent: Tuesday, December 06, 2005 8:43 PM
> Cc: swig-user <at> lists.sourceforge.net; swig-devel <at> lists.sourceforge.net
> Subject: [Swig-devel] Swig/Python thread support
> 
> Thread support was added to swig/python, based in the 
> Joseph's proposal.
> 
> Please test your interfaces using
> 
>      swig -threads -python ....
> 
> and send us a mail if you find a problem, or have 
> suggestions, comments, 
> etc.

Great stuff, thanks.

I've got a small suggestion though.  Please replace

#      define SWIG_PYTHON_THREAD_BEGIN_BLOCK   SWIG_Python_Thread_Block _swig_thread_block
#      define SWIG_PYTHON_THREAD_END_BLOCK     _swig_thread_block.end()
#      define SWIG_PYTHON_THREAD_BEGIN_ALLOW   SWIG_Python_Thread_Allow _swig_thread_allow
#      define SWIG_PYTHON_THREAD_END_ALLOW     _swig_thread_allow.end()

with

(Continue reading)

Marcelo Matus | 7 Dec 2005 19:34
Picon

Re: Swig/Python thread support

Ames Andreas wrote:

>>-----Original Message-----
>>From: swig-devel-admin <at> lists.sourceforge.net 
>>[mailto:swig-devel-admin <at> lists.sourceforge.net] On Behalf Of 
>>Marcelo Matus
>>Sent: Tuesday, December 06, 2005 8:43 PM
>>Cc: swig-user <at> lists.sourceforge.net; swig-devel <at> lists.sourceforge.net
>>Subject: [Swig-devel] Swig/Python thread support
>>
>>Thread support was added to swig/python, based in the 
>>Joseph's proposal.
>>
>>Please test your interfaces using
>>
>>     swig -threads -python ....
>>
>>and send us a mail if you find a problem, or have 
>>suggestions, comments, 
>>etc.
>>    
>>
>
>Great stuff, thanks.
>
>I've got a small suggestion though.  Please replace
>
>#      define SWIG_PYTHON_THREAD_BEGIN_BLOCK   SWIG_Python_Thread_Block _swig_thread_block
>#      define SWIG_PYTHON_THREAD_END_BLOCK     _swig_thread_block.end()
>#      define SWIG_PYTHON_THREAD_BEGIN_ALLOW   SWIG_Python_Thread_Allow _swig_thread_allow
(Continue reading)

Marcelo Matus | 7 Dec 2005 20:21
Picon

Re: [Swig-user] RE: Swig/Python thread support

Ok, I remembered the main reason why we can't do this:

#      define SWIG_PYTHON_THREAD_BEGIN_BLOCK   { SWIG_Python_Thread_Block _swig_thread_block
#      define SWIG_PYTHON_THREAD_END_BLOCK     }
#      define SWIG_PYTHON_THREAD_BEGIN_ALLOW   { SWIG_Python_Thread_Allow _swig_thread_allow
#      define SWIG_PYTHON_THREAD_END_ALLOW     }

the code should work in plain C, and we have some gotos inside there, ie:

{
  SWIG_PYTHON_THREAD_BEGIN_BLOCK;
  ...
     if (error) goto fail;
  ...
  SWIG_PYTHON_THREAD_END_BLOCK;
  return resultobj;
fail:
  SWIG_PYTHON_THREAD_END_BLOCK;
  return NULL;
}

also, it seems we need to leave the macro

#define SWIG_PYTHON_INITIALIZE_THREADS  PyEval_InitThreads()

or the test suite fails in many places. If needed, the user can do

  c++ -DSWIG_PYTHON_INITIALIZE_THREADS="" ...

and that cancels it. Anyway, the docs says it is save to call 
(Continue reading)

Louis Pecora | 8 Dec 2005 22:17
Picon
Picon

Solution to some SWIG Install problems on Mac OS X

If you are a newbie or non-guru (like me) and are are having some 
problems installing SWIG on Mac OS X (10.3.9, but version may not matter 
for the problems below), you should be aware of some problems I had and 
their solutions.  Many thanks to several people on the email lists for 
Pythonmac-sig (pythonmac-sig <at> python.org) and SWIG 
(swig-user <at> lists.sourceforge.net) for many valuable hints, clues and 
pointers.

The installation of SWIG on the Mac is the same three-step process as 
for Unix (% is the prompt):

% ./configure

% make

% make install

I had the follow problems with each step and I have the solutions.

(1) running ./configure resulted in a file not found.  When the script 
listed the file only a partial path name appeared.
   Solution:  directories (folders) and files with spaces and special 
Mac symbols (e.g. option-whatever) can cause problems.  I had some 
spaces in my folder names that held the SWIG files.  When I removed 
those and made more Unix-like names, ./configure worked (after one more 
fix below).

(2) running  'make'  caused a parse error in CParse when compiling 
cscanner.c.
    Solution:  when I looked back at ./configure I see that g++ was the 
(Continue reading)

Ames Andreas | 9 Dec 2005 14:55

RE: Swig/Python thread support

> -----Original Message-----
> From: Marcelo Matus [mailto:mmatus <at> acms.arizona.edu] 
> Sent: Wednesday, December 07, 2005 8:22 PM
> To: Ames Andreas
> Cc: swig-user <at> lists.sourceforge.net; swig-devel <at> lists.sourceforge.net
> Subject: Re: [Swig-user] RE: [Swig-devel] Swig/Python thread support
> 
> Ok, I remembered the main reason why we can't do this:
> 
> #      define SWIG_PYTHON_THREAD_BEGIN_BLOCK   { 
> SWIG_Python_Thread_Block _swig_thread_block
> #      define SWIG_PYTHON_THREAD_END_BLOCK     }
> #      define SWIG_PYTHON_THREAD_BEGIN_ALLOW   { 
> SWIG_Python_Thread_Allow _swig_thread_allow
> #      define SWIG_PYTHON_THREAD_END_ALLOW     }
> 
> the code should work in plain C, and we have some gotos 
> inside there, ie:
> 
> {
>   SWIG_PYTHON_THREAD_BEGIN_BLOCK;
>   ...
>      if (error) goto fail;
>   ...
>   SWIG_PYTHON_THREAD_END_BLOCK;
>   return resultobj;
> fail:
>   SWIG_PYTHON_THREAD_END_BLOCK;
>   return NULL;
> }
(Continue reading)

Marcelo Matus | 9 Dec 2005 18:54
Picon

Re: Swig/Python thread support

Ames Andreas wrote:

>>-----Original Message-----
>>From: Marcelo Matus [mailto:mmatus <at> acms.arizona.edu] 
>>Sent: Wednesday, December 07, 2005 8:22 PM
>>To: Ames Andreas
>>Cc: swig-user <at> lists.sourceforge.net; swig-devel <at> lists.sourceforge.net
>>Subject: Re: [Swig-user] RE: [Swig-devel] Swig/Python thread support
>>
>>Ok, I remembered the main reason why we can't do this:
>>
>>#      define SWIG_PYTHON_THREAD_BEGIN_BLOCK   { 
>>SWIG_Python_Thread_Block _swig_thread_block
>>#      define SWIG_PYTHON_THREAD_END_BLOCK     }
>>#      define SWIG_PYTHON_THREAD_BEGIN_ALLOW   { 
>>SWIG_Python_Thread_Allow _swig_thread_allow
>>#      define SWIG_PYTHON_THREAD_END_ALLOW     }
>>
>>the code should work in plain C, and we have some gotos 
>>inside there, ie:
>>
>>{
>>  SWIG_PYTHON_THREAD_BEGIN_BLOCK;
>>  ...
>>     if (error) goto fail;
>>  ...
>>  SWIG_PYTHON_THREAD_END_BLOCK;
>>  return resultobj;
>>fail:
>>  SWIG_PYTHON_THREAD_END_BLOCK;
(Continue reading)

Marcelo Matus | 9 Dec 2005 19:58
Picon

Re: Swig/Python thread support

Oh, well, still we have the dispatch case:
{
  SWIG_PYTHON_THREAD_BEGIN_BLOCK;
  argc = PyObject_Length(args);
  for (ii = 0; (ii < argc) && (ii < 2); ii++) {
    argv[ii] = PyTuple_GetItem(args,ii);
  }
  if (argc == 1) {
    int _v;
    _v = (SWIG_AsVal_int(argv[0], 0) == SWIG_OK);
    if (_v) {
      SWIG_PYTHON_THREAD_END_BLOCK;
      return _wrap_overtparams1__SWIG_2(self,args);
    }
  }
  ....
  SWIG_PYTHON_THREAD_END_BLOCK;
  SWIG_SetErrorMsg(PyExc_NotImplementedError,"No matching function for 
overloaded 'overtparams1'");
  return NULL;
}

the problem is that  eliminating SWIG_PYTHON_THREAD_END_BLOCK in C++
will produce different results in C and C++, ie, in the first case the 
blocks
is ended before dispatching the method, in the second, the block will be
closed after the call.  In theory we can change the code to produce
a more friendly code for threads, but the dispatch generator is
common in C++ and is not easy to change since
it must work for all the languages.
(Continue reading)

Ames Andreas | 9 Dec 2005 20:14

RE: Swig/Python thread support

> -----Original Message-----
> From: Marcelo Matus [mailto:mmatus <at> acms.arizona.edu] 
> Sent: Friday, December 09, 2005 6:54 PM
> To: Ames Andreas
> Cc: swig-user <at> lists.sourceforge.net; swig-devel <at> lists.sourceforge.net
> Subject: Re: [Swig-devel] Swig/Python thread support
> 
> but we call PyEval_InitThreads only when the module is 
> loaded/imported, 
> is that still dangerous?
> 
> I mean, if you can't never call it safely, nobody should call 
> it ever, 
> right?

Ok, it seems I've found a good reference for the problem at hand, please take a look at
http://www.python.org/peps/pep-0311.html.  According to this, what you are doing with
PyEval_InitThreads seems to be the right thing.  Sorry for the fuzz.

Calling it in the init code should ensure that it is either called from python or from a thread holding the GIL
(anything else would be a bug in the calling code).  If it's called from a thread holding the GIL, it is
essentially a noop (see Python/ceval.c); if it's called from python, it is either called from a thread
that holds the GIL, so see above, or there is no GIL yet, which actually means there is no other python thread
(because thread creation calls PyEval_InitThreads).  In this case it is reasonable to confer the 'main
thread' badge on the calling thread.  Once again, sorry for wasting your time.

cheers,

aa

(Continue reading)


Gmane