> ## Documentation Index
> Fetch the complete documentation index at: https://developer.synopsys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# shared_memory_image_client_python.c

[Go to the documentation of this file.](https://developer.synopsys.com/docs/ensight/ensight-apis/2025-r1/shared__memory__image__client__python_8c)

```c theme={null}
/**************************************************************************
 *
 *   ****************************************
 *    Copyright 2020 ANSYS Inc.
 *    All Rights Reserved.
 *
 *    Restricted Rights Legend
 *
 *    Government is subject to restrictions as
 *    set forth in subdivision [(b)(3)(ii)] of
 *    the Rights in Technical Data and Computer
 *    Software clause at 52.227-7013.
 *   ****************************************
 *
 *************************************************************************/

/* Warning this is a HACK to get around Python issues mixing debug modules with
 * release Python builds. */

#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif

#include "shared_memory_image_client_priv.h"

#define MAX_STREAMS 50
static SharedMemoryImageStream streamlist[MAX_STREAMS] = { 0 };
#ifdef DOXYGEN
stream_id stream_create(str filename, int options=IMAGESTREAM_OPTIONS_NONE);

frame stream_lock(stream_id id);

SharedMemoryImageError stream_unlock(stream_id id);

SharedMemoryImageError stream_destroy(stream_id id);

SharedMemoryImageError stream_putframe(stream_id id, int width, int height, int framenumber, bytes pixeldata);

#endif

/* Utility functions PyDict_SetItemString with DECREF */
static int PyDict_SetItemString_DECREF(PyObject *dict, const char *key, PyObject *val)
{
 int ret = PyDict_SetItemString(dict, key, val);
 if (ret == 0) Py_DECREF(val);
 return(ret);
}

// Given an error code, return the code or raise an exception
static PyObject *return_error(int err)
{
 if (err >= IMAGESTREAM_NOERROR) {
 return PyLong_FromLong(err);
    }
    PyErr_SetString(PyExc_RuntimeError, SharedMemoryImageStream_errorstring(err));
 return NULL;
}

static PyObject *Py_stream_create(PyObject *self, PyObject *args, PyObject *keywds)
{
 char *filename = NULL;
    uint32_t options = 0;
 static const char *kwlist[] = { "filename", "options", NULL };
 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"s|i", (char **)kwlist,
        &filename, &options)) return NULL;
    int32_t stream_id = -1;
 for (int i = 0; i < MAX_STREAMS; i++) {
 if (!streamlist[i]) {
            stream_id = i;
        }
    }
 if (stream_id == -1) {
        PyErr_SetString(PyExc_RuntimeError, "Maximum number of simultaneous streams open");
 return NULL;
    }
 SharedMemoryImageStream stream;
 SharedMemoryImageError err = SharedMemoryImageStream_create(filename, options, &stream);
 if (err != IMAGESTREAM_NOERROR) {
 return return_error(err);
    }
    streamlist[stream_id] = stream;
 return PyLong_FromLong(stream_id);
}

static PyObject *Py_stream_lock(PyObject *self, PyObject *args, PyObject *keywds)
{
    int32_t stream_id = -1;
 static const char *kwlist[] = { "stream_id", NULL };
 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"i", (char **)kwlist,
        &stream_id)) return NULL;
 if ((stream_id < 0) || (stream_id >= MAX_STREAMS) || !streamlist[stream_id]) {
        PyErr_SetString(PyExc_RuntimeError, "Unknown stream id");
 return NULL;
    }
 SharedMemoryFrame frame;
 SharedMemoryImageError err = SharedMemoryImageStream_lock(streamlist[stream_id], &frame);
 if (err != IMAGESTREAM_NOERROR) {
 return return_error(err);
    }
    PyObject *ret = PyDict_New();
    PyDict_SetItemString_DECREF(ret, "width", PyLong_FromLong(frame->iWidth));
    PyDict_SetItemString_DECREF(ret, "height", PyLong_FromLong(frame->iHeight));
    PyDict_SetItemString_DECREF(ret, "framenumber", PyLong_FromLong(frame->iFrame));
    PyDict_SetItemString_DECREF(ret, "pixeldata", PyBytes_FromStringAndSize(frame->buffer,
        frame->iWidth*3*frame->iHeight));
 return ret;
}

static PyObject *Py_stream_unlock(PyObject *self, PyObject *args, PyObject *keywds)
{
    int32_t stream_id = -1;
 static const char *kwlist[] = { "stream_id", NULL };
 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"i", (char **)kwlist,
        &stream_id)) return NULL;
 if ((stream_id < 0) || (stream_id >= MAX_STREAMS) || !streamlist[stream_id]) {
        PyErr_SetString(PyExc_RuntimeError, "Unknown stream id");
 return NULL;
    }
 SharedMemoryImageError err = SharedMemoryImageStream_unlock(streamlist[stream_id]);
 return return_error(err);
}

static PyObject *Py_stream_putframe(PyObject *self, PyObject *args, PyObject *keywds)
{
    int32_t stream_id = -1;
    int32_t iWidth = 0;
    int32_t iHeight = 0;
    uint32_t iFrame = 0;
    PyObject *pData = NULL;
 static const char *kwlist[] = { "stream_id", "width", "height", "framenumber", "pixeldata", NULL };
 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"iiiiO", (char **)kwlist,
        &stream_id, &iWidth, &iHeight, &iFrame, &pData)) return NULL;
 if ((stream_id < 0) || (stream_id >= MAX_STREAMS) || !streamlist[stream_id]) {
        PyErr_SetString(PyExc_RuntimeError, "Unknown stream id");
 return NULL;
    }
    int32_t frame_size = iWidth * iHeight * 3;
 if (frame_size < 1) {
        PyErr_SetString(PyExc_RuntimeError, "Invalid frame size");
 return NULL;
    }
 if (!PyBytes_Check(pData)) {
        PyErr_SetString(PyExc_RuntimeError, "Pixeldata must be a bytes object");
 return NULL;
    }
    Py_ssize_t data_size = PyBytes_Size(pData);
 if (data_size < frame_size) {
        PyErr_SetString(PyExc_RuntimeError, "Pixeldata is too small for specified frame size");
 return NULL;
    }
 struct _SharedMemoryFrame frame;
    frame.iFrame = iFrame;
    frame.iWidth = iWidth;
    frame.iHeight = iHeight;
    frame.buffer = PyBytes_AsString(pData);
 SharedMemoryImageError err = SharedMemoryImageStream_putframe(streamlist[stream_id], &frame);
 return return_error(err);
}

static PyObject *Py_stream_destroy(PyObject *self, PyObject *args, PyObject *keywds)
{
    int32_t stream_id = -1;
 static const char *kwlist[] = { "stream_id", NULL };
 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"i", (char **)kwlist,
        &stream_id)) return NULL;
 if ((stream_id < 0) || (stream_id >= MAX_STREAMS) || !streamlist[stream_id]) {
        PyErr_SetString(PyExc_RuntimeError, "Unknown stream id");
 return NULL;
    }
 SharedMemoryImageError err = SharedMemoryImageStream_destroy(streamlist[stream_id]);
    streamlist[stream_id] = NULL;
 return return_error(err);
}

static void stream_python_shutdown(void)
{
 for (int i = 0; i < MAX_STREAMS; i++) {
 if (streamlist[i]) SharedMemoryImageStream_destroy(streamlist[i]);
        streamlist[i] = NULL;
    }
}

/* Module definition */
static PyObject *pmodule = NULL;
static PyMethodDef py_methods[] = {
    {(char *)"stream_create", (PyCFunction)Py_stream_create, METH_VARARGS | METH_KEYWORDS,
     (char *)"Create a new shared memory image stream"},
    {(char *)"stream_lock", (PyCFunction)Py_stream_lock, METH_VARARGS | METH_KEYWORDS,
     (char *)"Lock and return the next image from an image stream"},
    {(char *)"stream_unlock", (PyCFunction)Py_stream_unlock, METH_VARARGS | METH_KEYWORDS,
     (char *)"Release the lock on an image stream"},
    {(char *)"stream_putframe", (PyCFunction)Py_stream_putframe, METH_VARARGS | METH_KEYWORDS,
     (char *)"Push a new frame into the image stream"},
    {(char *)"stream_destroy", (PyCFunction)Py_stream_destroy, METH_VARARGS | METH_KEYWORDS,
     (char *)"Destroy an image stream"},
    {NULL, NULL, 0, NULL}
};

#if PY_MAJOR_VERSION >= 3

static struct PyModuleDef moduledef = {
        PyModuleDef_HEAD_INIT,
 "ensight_grpc_shmem",
 "EnSight gRPC shared memory image stream",
        -1,
        py_methods,
        NULL,                /* m_reload */
        NULL,                /* m_traverse */
        NULL,                /* m_clear */
        NULL,                /* m_free */
};

PyMODINIT_FUNC PyInit_ensight_grpc_shmem()
{
    pmodule = PyModule_Create(&moduledef);
#else
PyMODINIT_FUNC initensight_grpc_shmem()
{
    pmodule = Py_InitModule3("ensight_grpc_shmem", py_methods, "EnSight gRPC shared memory image stream");
#endif

 // Common (2 & 3) code starts here -------------------------------------------------
    PyModule_AddStringConstant(pmodule, "version", IMAGESTREAM_API_VERSION);

    PyModule_AddIntConstant(pmodule, "OPTIONS_NONE", (int)IMAGESTREAM_OPTIONS_NONE);
    PyModule_AddIntConstant(pmodule, "OPTIONS_SERVER", (int)IMAGESTREAM_OPTIONS_SERVER);
    PyModule_AddIntConstant(pmodule, "OPTIONS_FLIPVERTICAL", (int)IMAGESTREAM_OPTIONS_FLIPVERTICAL);

    PyModule_AddIntConstant(pmodule, "ERROR_NOERROR", (int)IMAGESTREAM_NOERROR);
    PyModule_AddIntConstant(pmodule, "ERROR_NO_BUFFERAVAILABLE", (int)IMAGESTREAM_NO_BUFFERAVAILABLE);
    PyModule_AddIntConstant(pmodule, "ERROR_NO_FRAMEAVAILABLE", (int)IMAGESTREAM_NO_FRAMEAVAILABLE);
    PyModule_AddIntConstant(pmodule, "ERROR_RECONNECTING", (int)IMAGESTREAM_RECONNECTING);
    PyModule_AddIntConstant(pmodule, "ERROR_UNCONNECTED", (int)IMAGESTREAM_UNCONNECTED);

 /* cleanup on exit */
    Py_AtExit(stream_python_shutdown);
 // Common (2 & 3) code ends here -------------------------------------------------

#if PY_MAJOR_VERSION >= 3
 return pmodule;
}
#else
 return;
}
#endif
```
