Need advice on the design of my application

Chris Kaynor ckaynor at zindagigames.com
Wed Dec 21 14:37:34 EST 2011


On Wed, Dec 21, 2011 at 11:19 AM, hbd666 <happybrowndog at hotmail.com> wrote:

> <snip>
>
> In my experience implementing Option 1 in another project, I know that
> Python suspends
> execution until the DLL function calls return, but I did not launch the
> DLL on a thread.
> I expect that if the DLL were launched on a thread, a function call into
> the DLL will still
> suspend Python.  Maybe someone can tell me if this is true?
>
> Option 2 is of most interest to me, but how shall I handle the Python GIL
> when the Renderer
> runs its main loop?  Will the main loop be unaffected because Python
> interpreter is embedded
> in a thread?


When extending Python (as option 1 would do), the extension would block all
Python threads if called from Python (though c-threads created by the
module would not block Python). You can explicitly release the GIL during
your C code's execution, which would cause only the thread actually making
the call to be blocked (as it is actually running the C code), but other
Python threads would still be able to run.

When embedding Python (as option 2 would do), the embedding program will
not be affected by the GIL unless it explicitly acquires the GIL (which is
must do before accessing Python).


Via the Python API functions you can acquire and release the Python GIL as
needed. The only rule is that you must hold the Python GIL should you wish
to access any part of the Python interpreter (function calls, variables,
etc) [note: technically you don't HAVE to, but you run a high risk of data
corruption and segfaults from the race conditions which would exist].


Either option should work fine.

In case of option 1:

   1. Import the C module.
   2. The C module sets up some defaults for the render.
   3. The C module creates its render thread (which is unaffected by the
   GIL, but probably needs other locks around global variables).
   4. The C module returns to Python.
   5. The Python GUI calls into the C module to change settings (which
   probably need to acquire short-term locks)

An alternative would be:

   1. Import the C module.
   2. The C module setups up some defaults.
   3. Create a Python thread to call the C module's render loop.
   4. The render loop thread releases the GIL and continues as above.
   5. The main Python thread continues as above.


For option 2:

   1. Initialize any needed variables for the system.
   2. Start a new thread for loading and executing Python.
   3. Have the main thread start the render loop.
   4. Have the Python thread startup up Python.
   5. Continue as in the first case above.



>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111221/c95b96ed/attachment-0001.html>


More information about the Python-list mailing list