msl.examples.loadlib.cpp64 module
Communicates with cpp_lib32 via the Cpp32
class.
Example of a module that can be executed within a 64-bit Python interpreter which can
communicate with a 32-bit library, cpp_lib32, that is hosted
by a 32-bit Python server, cpp32
. A 64-bit process cannot load a
32-bit library and therefore inter-process communication is used to
interact with a 32-bit library from a 64-bit process.
Cpp64
is the 64-bit client and Cpp32
is the 32-bit server for inter-process communication.
- class msl.examples.loadlib.cpp64.Cpp64[source]
Bases:
Client64
Communicates with a 32-bit C++ library, cpp_lib32.
This class demonstrates how to communicate with a 32-bit C++ library if an instance of this class is created within a 64-bit Python interpreter.
Base class for communicating with a 32-bit library from 64-bit Python.
Starts a 32-bit server,
Server32
, to host a Python class that is a wrapper around a 32-bit library.Client64
runs within a 64-bit Python interpreter, and it sends a request to the server which calls the 32-bit library to execute the request. The server then provides a response back to the client.Changed in version 0.6: Added the rpc_timeout argument.
Changed in version 0.8: Added the protocol argument and the default quiet value became
None
.Changed in version 0.10: Added the server32_dir argument.
- Parameters:
module32 (
str
) – The name of the Python module that is to be imported by the 32-bit server.host (
str
, optional) – The address of the 32-bit server. Default is'127.0.0.1'
.port (
int
, optional) – The port to open on the 32-bit server. Default isNone
, which means to automatically find a port that is available.timeout (
float
, optional) – The maximum number of seconds to wait to establish a connection to the 32-bit server. Default is 10 seconds.quiet (
bool
, optional) – This keyword argument is no longer used and will be removed in a future release.append_sys_path (
str
orlist
ofstr
, optional) – Append path(s) to the 32-bit server’ssys.path
variable. The value ofsys.path
from the 64-bit process is automatically included, i.e.,sys.path(32bit) = sys.path(64bit) + append_sys_path
.append_environ_path (
str
orlist
ofstr
, optional) – Append path(s) to the 32-bit server’sos.environ['PATH']
variable. This can be useful if the library that is being loaded requires additional libraries that must be available onPATH
.rpc_timeout (
float
, optional) – The maximum number of seconds to wait for a response from the 32-bit server. The RPC timeout value is used for all requests from the server. If you want different requests to have different timeout values then you will need to implement custom timeout handling for each method on the server. Default isNone
, which means to use the default timeout value used by thesocket
module (which is to wait forever).protocol (
int
, optional) – Thepickle
protocol to use. If not specified then determines the value to use based on the version of Python that theClient64
is running in.server32_dir (
str
, optional) – The directory where the frozen 32-bit server is located.**kwargs – All additional keyword arguments are passed to the
Server32
subclass. The data type of each value is not preserved. It will be a string at the constructor of theServer32
subclass.
Note
If module32 is not located in the current working directory then you must either specify the full path to module32 or you can specify the folder where module32 is located by passing a value to the append_sys_path parameter. Using the append_sys_path option also allows for any other modules that module32 may depend on to also be included in
sys.path
so that those modules can be imported when module32 is imported.- Raises:
ConnectionTimeoutError – If the connection to the 32-bit server cannot be established.
OSError – If the frozen executable cannot be found.
TypeError – If the data type of append_sys_path or append_environ_path is invalid.
- subtract(a, b)[source]
Subtract two floating-point numbers (‘float’ refers to the C++ data type).
See the corresponding 32-bit
subtract()
method.
- add_or_subtract(a, b, do_addition)[source]
Add or subtract two floating-point numbers (‘double’ refers to the C++ data type).
See the corresponding 32-bit
add_or_subtract()
method.
- scalar_multiply(a, xin)[source]
Multiply each element in an array by a number.
See the corresponding 32-bit
scalar_multiply()
method.
- reverse_string_v1(original)[source]
Reverse a string (version 1).
In this method Python allocates the memory for the reversed string and passes the string to C++.
See the corresponding 32-bit
reverse_string_v1()
method.
- reverse_string_v2(original)[source]
Reverse a string (version 2).
In this method C++ allocates the memory for the reversed string and passes the string to Python.
See the corresponding 32-bit
reverse_string_v2()
method.
- distance_4_points(points)[source]
Calculates the total distance connecting 4
Point
’s.See the corresponding 32-bit
distance_4_points()
method.Attention
This method does not work with if
Cpp64
is running in Python 2. You would have to create theFourPoints
object in the 32-bit version ofdistance_4_points()
because there are issues using thepickle
module between different major version numbers of Python forctypes
objects.- Parameters:
points (
FourPoints
) – Since points is a struct that is a fixed size we can pass thectypes.Structure
object directly from 64-bit Python to the 32-bit Python. Thectypes
module on the 32-bit server can load thepickle
’dctypes.Structure
.- Returns:
- circumference(radius, n)[source]
Estimates the circumference of a circle.
This method calls the
distance_n_points
function in cpp_lib32.See the corresponding 32-bit
circumference()
method.