Callback from c thread with ctypes

2009-03-08 Thread Victor Lin
Hi,

I am going to develop a c library binding with ctypes. That c library
will call callback from worker threads it created. Here comes the
problem : Will the GIL be acquired before it goes into Python
function?

I got a little try..

DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)

def test(handle, channel, buffer, length, user):
print handle, channel, buffer, length, user

dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)

I got access violation when I run it... It seems that the ctypes
did't acquire GIL before it call the python callback. As the document
says.

WINFUNCTYPE will release GIL during the call

But it does not mention callback about Python function? How about a
call from another thread? Could somebody help me?

Thanks.
Victor Lin.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Diez B. Roggisch

Victor Lin schrieb:

Hi,

I am going to develop a c library binding with ctypes. That c library
will call callback from worker threads it created. Here comes the
problem : Will the GIL be acquired before it goes into Python
function?

I got a little try..

DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)

def test(handle, channel, buffer, length, user):
print handle, channel, buffer, length, user

dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)

I got access violation when I run it... It seems that the ctypes
did't acquire GIL before it call the python callback. As the document
says.

WINFUNCTYPE will release GIL during the call

But it does not mention callback about Python function? How about a
call from another thread? Could somebody help me?


The releasing takes only place when entering a c-function. A 
python-callback acquires the GIL, see callbacks.c in the python source.


The access violation has nothing to do with that I presume, that's just 
a general programming error as it happens with ctypes during development.


Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Christian Heimes
Victor Lin wrote:
 Hi,
 
 I am going to develop a c library binding with ctypes. That c library
 will call callback from worker threads it created. Here comes the
 problem : Will the GIL be acquired before it goes into Python
 function?
 
 I got a little try..
 
 DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)
 
 def test(handle, channel, buffer, length, user):
 print handle, channel, buffer, length, user
 
 dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)
 
 I got access violation when I run it... It seems that the ctypes
 did't acquire GIL before it call the python callback. As the document
 says.
 
 WINFUNCTYPE will release GIL during the call
 
 But it does not mention callback about Python function? How about a
 call from another thread? Could somebody help me?


You can't call Python code from an arbitrary thread. Before you are
allowed to call a Python function from a thread, you must enable
Python's threading subsystem and register the thread. Python need to
store information (like the exception information) in a thread local
variable (TLS).

I can't tell you how to register your thread with Python, though.

Christian

--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Victor Lin
On 3月8日, 下午9時56分, Diez B. Roggisch de...@nospam.web.de wrote:
 Victor Lin schrieb:



  Hi,

  I am going to develop a c library binding with ctypes. That c library
  will call callback from worker threads it created. Here comes the
  problem : Will the GIL be acquired before it goes into Python
  function?

  I got a little try..

  DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)

  def test(handle, channel, buffer, length, user):
  print handle, channel, buffer, length, user

  dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)

  I got access violation when I run it... It seems that the ctypes
  did't acquire GIL before it call the python callback. As the document
  says.

  WINFUNCTYPE will release GIL during the call

  But it does not mention callback about Python function? How about a
  call from another thread? Could somebody help me?

 The releasing takes only place when entering a c-function. A
 python-callback acquires the GIL, see callbacks.c in the python source.

 The access violation has nothing to do with that I presume, that's just
 a general programming error as it happens with ctypes during development.

 Diez

Hi,

Thanks your replying, I try it again, found that my program only crash
when I call it from python's IDLE. If I click it, namely execute it
with python directly, it works fine. Why the program will crash within
IDLE?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Victor Lin
On 3月8日, 下午10時20分, Christian Heimes li...@cheimes.de wrote:
 Victor Lin wrote:
  Hi,

  I am going to develop a c library binding with ctypes. That c library
  will call callback from worker threads it created. Here comes the
  problem : Will the GIL be acquired before it goes into Python
  function?

  I got a little try..

  DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)

  def test(handle, channel, buffer, length, user):
  print handle, channel, buffer, length, user

  dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)

  I got access violation when I run it... It seems that the ctypes
  did't acquire GIL before it call the python callback. As the document
  says.

  WINFUNCTYPE will release GIL during the call

  But it does not mention callback about Python function? How about a
  call from another thread? Could somebody help me?

 You can't call Python code from an arbitrary thread. Before you are
 allowed to call a Python function from a thread, you must enable
 Python's threading subsystem and register the thread. Python need to
 store information (like the exception information) in a thread local
 variable (TLS).

 I can't tell you how to register your thread with Python, though.

 Christian

I know I have to call PyEval_InitThreads if my module create threads
that will contact python stuff, for example, call a python callback
function from threads. But however, it is ctypes. I have no idea
should I do that for the imported dll? If it is, how?

Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Christian Heimes
Victor Lin wrote
 I know I have to call PyEval_InitThreads if my module create threads
 that will contact python stuff, for example, call a python callback
 function from threads. But however, it is ctypes. I have no idea
 should I do that for the imported dll? If it is, how?

You have to initialize the thread state for the current thread, too. See
Python/pystate.c:PyThreadState_New()

--
http://mail.python.org/mailman/listinfo/python-list


Re: Callback from c thread with ctypes

2009-03-08 Thread Lie Ryan

Victor Lin wrote:

On 3月8日, 下午9時56分, Diez B. Roggisch de...@nospam.web.de wrote:

Victor Lin schrieb:




Hi,
I am going to develop a c library binding with ctypes. That c library
will call callback from worker threads it created. Here comes the
problem : Will the GIL be acquired before it goes into Python
function?
I got a little try..
DSPPROC = WINFUNCTYPE(None, DWORD, DWORD, c_void_p, DWORD, c_void_p)
def test(handle, channel, buffer, length, user):
print handle, channel, buffer, length, user
dsp = BASS_ChannelSetDSP(stream, DSPPROC(test), None, 123)
I got access violation when I run it... It seems that the ctypes
did't acquire GIL before it call the python callback. As the document
says.
WINFUNCTYPE will release GIL during the call
But it does not mention callback about Python function? How about a
call from another thread? Could somebody help me?

The releasing takes only place when entering a c-function. A
python-callback acquires the GIL, see callbacks.c in the python source.

The access violation has nothing to do with that I presume, that's just
a general programming error as it happens with ctypes during development.

Diez


Hi,

Thanks your replying, I try it again, found that my program only crash
when I call it from python's IDLE. If I click it, namely execute it
with python directly, it works fine. Why the program will crash within
IDLE?


Usually because IDLE is written in python and Tkinter. IDLE doesn't give 
a completely isolated environment for your program to run in, this 
causes python program that uses Tkinter often causes various 
undescribable oddities.

--
http://mail.python.org/mailman/listinfo/python-list