Shared ObjectsΒΆ

We will want several instances of the typesetting daemon, each with their own set of macros and fonts loaded. It is easy to link shared objects to Python scripts by using the ctypes module. But they are still shared objects, and so they share state.

/* Show that a shared object has shared stated. */

int counter = 0;

int count() {

  return counter ++;
}
# We show that shared objects share data, and suggest a possible way
# that a process might be used as a shared object.

from ctypes import *

SO = './tp_count.so'

sos = [
    cdll.LoadLibrary(SO),
    cdll.LoadLibrary(SO)
]

assert sos[0]._handle == sos[1]._handle

for so in sos:
    so.restype = c_int


data = [sos[0].count() for i in range(3)] \
    + [sos[1].count() for i in range(3)] \
    + [sos[1].count() for i in range(3)]

assert data == range(9)

# According to http://linux.die.net/man/3/dlopen, if NULL is passed to
# dlopen() as the name of the shared object to load then "the returned
# handle is for the main program."  This might allow a process to be
# used as if it were a shared object.

from_handle = CDLL(name='guess', handle=sos[0]._handle)

assert [from_handle.count() for i in range(3)] == range(9, 12)

print 'ok'

Project Versions

Previous topic

Toy copy

Next topic

texlike-daemon

This Page