next up previous
Next: 4 Performance Up: 3 Example Usage Previous: 3.1 Basic Usage

3.2 Using POSH with User-Defined Types


\begin{center}\vbox{\input{sources/Parrot.py.verbatim}
}\end{center}
Figure 4: Definition of a shareable Parrot type.


\begin{center}\vbox{\input{sources/ParrotSession.py.verbatim}
}\end{center}
Figure 5: Interpreter session using a shared Parrot object.

In addition to the standard Python types, types defined by the user (using the class statement) can also be made shareable. These types have to be registered with POSH using the posh.allow_sharing function, which will create an appropriate shared type through sub-typing. User-defined shareable types have to be registered prior to any posh.fork() calls, in order for the shared types to remain accessible to all processes.

For instance, Figure 4 shows the definition of a new Parrot type whose instances will be shareable. There is nothing special about the type definition in itself -- the only point of interest is the call to allow_sharing following the class statement. The arguments to allow_sharing are the shareable type and an optional initializer function. The latter is a function that specifies how to create a new instance of the shared type given an instance of the shareable type. In this particular example, the function specifies how to create a shared Parrot object from an existing non-shared Parrot object. The function used in the example, posh.generic_init, will be appropriate for many user-defined types. It assigns all the attributes of the non-shared object to a new shared object. The user may freely define new initializer functions as needed.

An interpreter session that illustrates the use of a shared Parrot object is listed in Figure 5. The final line reveals that the object, while behaving exactly like a Parrot object, is in fact a proxy object for a shared object. The attributes that are assigned to shared objects are implicitly shared, just like the items assigned to shared lists and dictionaries.


next up previous
Next: 4 Performance Up: 3 Example Usage Previous: 3.1 Basic Usage