Panda3D Manual: Threading
<<prev | top | next>> |
Note: In versions 1.6.0 and above, Panda3D provides a safe threading interface you can use, which works very similar to Python's threading modules. It is not recommended to use threading before version 1.6 or to use Python's threading modules, since your application will then become highly unstable. ## Threading If you want to use threading with Panda3D, it's not recommended to use Python's built-in threading modules, since you will most likely run into issues (for Panda3D is written in C++ and thus does not use the Python threading modules). However, Panda3D offers a threading implementation that is safe to use, by reimplementing Python's "thread" and "threading" modules, these work the same as the Python built-in threading modules but are actually safe to use with Panda3D. You can get access to Panda3D's implementation of Python's "thread" module by importing the "thread" module from direct.stdpy:
    # WRONG:
    import thread
    # RIGHT:
    from direct.stdpy import thread
For the Python module "threading", Panda3D offers two equivalents, "threading" and "threading2", which you can find both in direct.stdpy also. The "threading" module implements the threading module with a thin layer over Panda's threading constructs. As such, the semantics are close to, but not precisely, the semantics documented for Python's standard threading module. If you really do require strict adherence to Python's semantics, see the threading2 module instead. In fact, the threading2 module is a bald-face copy of Python's threading module from Python 2.5, with a few lines at the top to import Panda's thread reimplementation instead of the system thread module, and so it is therefore layered on top of Panda's thread implementation. However, if you don't need such strict adherence to Python's original semantics, the "threading" module is probably a better choice. It is likely to be slightly faster than the threading2 module (and even slightly faster than Python's own threading module). It is also better integrated with Panda's threads, so that Panda's thread debug mechanisms will be easier to use and understand.
    # WRONG:
    import threading
    # RIGHT:
    from direct.stdpy import threading
    # ALSO RIGHT:
    from direct.stdpy import threading2 as threading
It is permissible to mix-and-match both threading and threading2 within the same application. ## File I/O In versions 1.6.0 and above, Panda3D also offers a thread-safe replacement for the Python file module. You can find it in direct.stdpy.file. The interface is exactly the same as Python's, so it's safe to put this import above all the files where you want to use the "file" or "open" functions:
    from direct.stdpy.file import *
This module reimplements Python's file I/O mechanisms using Panda constructs. This enables Python to interface more easily with Panda's virtual file system, and it also better-supports Panda's SIMPLE_THREADS model, by avoiding blocking all threads while waiting for I/O to complete. ## Compiling Panda3D with threading support There are two different interfaces for threading which you can enable using the definitions HAVE_THREADS and SIMPLE_THREADS. The former is a full and heavy implementation of threading and compiling with that option will slow down the Panda3D build, unless you fully make use of the benefits that threading gives. The latter, however, is a more simple threading interface (introduced in Panda3D 1.6.0) that doesn't give you the runtime overhead HAVE_THREADS gives you. In Panda3D 1.6.0 and higher, SIMPLE_THREADS is enabled in the default build. In the makepanda system, SIMPLE_THREADS is automatically defined for you. If you don't want it, or want to use the more heavy HAVE_THREADS instead, you will need to edit the makepanda.py script and change the definitions. If you use ppremake, you will just need to define either in your Config.pp file.
<<prev | top | next>> |