Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions sample_apps/advanced_example_py/advanced_example1.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import os
import sys
import ctypes
from dataclasses import dataclass

JRTC_APP_PATH = os.environ.get("JRTC_APP_PATH")
if JRTC_APP_PATH is None:
Expand All @@ -21,18 +21,13 @@

##########################################################################
# Define the state variables for the application
class AppStateVars(ctypes.Structure):
_fields_ = [
("app", ctypes.POINTER(JrtcApp)),

# add custom fields below
("agg_cnt", ctypes.c_int32)
]

@dataclass
class AppStateVars:
app: JrtcApp
agg_cnt: int

##########################################################################
# Handler callback function (this function gets called by the C library)
def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(struct_jrtc_router_data_entry), state_ptr: int):
def app_handler(timeout: bool, stream_idx: int, data_entry: struct_jrtc_router_data_entry, state: AppStateVars):

GENERATOR_OUT_STREAM_IDX = 0
APP2_OUT_STREAM_IDX = 1
Expand All @@ -45,10 +40,6 @@ def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(s

else:

# Dereference the pointer arguments
state = ctypes.cast(state_ptr, ctypes.POINTER(AppStateVars)).contents
data_entry = data_entry_ptr.contents

if stream_idx == GENERATOR_OUT_STREAM_IDX:
# Extract data from the received entry
data = ctypes.cast(data_entry.data, ctypes.POINTER(example_msg)).contents
Expand Down Expand Up @@ -130,7 +121,7 @@ def jrtc_start_app(capsule):
)

# Initialize the app
state = AppStateVars(agg_cnt=0)
state = AppStateVars(agg_cnt=0, app=None)
state.app = jrtc_app_create(capsule, app_cfg, app_handler, state)

# run the app - This is blocking until the app exits
Expand Down
23 changes: 8 additions & 15 deletions sample_apps/advanced_example_py/advanced_example2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import os
import sys
import ctypes
from dataclasses import dataclass

JRTC_APP_PATH = os.environ.get("JRTC_APP_PATH")
if JRTC_APP_PATH is None:
Expand All @@ -21,18 +21,14 @@

##########################################################################
# Define the state variables for the application
class AppStateVars(ctypes.Structure):
_fields_ = [
("app", ctypes.POINTER(JrtcApp)),

# add custom fields below
("agg_cnt", ctypes.c_int32),
("received_counter", ctypes.c_int)
]
@dataclass
class AppStateVars:
app: JrtcApp
agg_cnt: int
received_counter: int

##########################################################################
# Handler callback function (this function gets called by the C library)
def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(struct_jrtc_router_data_entry), state_ptr: int):
def app_handler(timeout: bool, stream_idx: int, data_entry: struct_jrtc_router_data_entry, state: AppStateVars):

GENERATOR_PB_OUT_STREAM_IDX = 0
APP2_OUT_STREAM_IDX = 1
Expand All @@ -43,9 +39,6 @@ def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(s
pass

else:
# Dereference the pointer arguments
state = ctypes.cast(state_ptr, ctypes.POINTER(AppStateVars)).contents
data_entry = data_entry_ptr.contents

if stream_idx == GENERATOR_PB_OUT_STREAM_IDX:

Expand Down Expand Up @@ -135,7 +128,7 @@ def jrtc_start_app(capsule):
)

# Initialize the app
state = AppStateVars(agg_cnt=0, received_counter=0)
state = AppStateVars(agg_cnt=0, received_counter=0, app=None)
state.app = jrtc_app_create(capsule, app_cfg, app_handler, state)

# run the app - This is blocking until the app exists
Expand Down
28 changes: 11 additions & 17 deletions sample_apps/first_example_py/first_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import os
import sys
import ctypes
from dataclasses import dataclass

JRTC_APP_PATH = os.environ.get("JRTC_APP_PATH")
if JRTC_APP_PATH is None:
Expand All @@ -17,22 +17,20 @@
simple_input = sys.modules.get('simple_input')
from generated_data import example_msg
from simple_input import simple_input
from jrtc_bindings import (
struct_jrtc_router_data_entry,
)

##########################################################################
# Define the state variables for the application
class AppStateVars(ctypes.Structure):
_fields_ = [
("app", ctypes.POINTER(JrtcApp)),

# add custom fields below
("agg_cnt", ctypes.c_int),
("received_counter", ctypes.c_int)
]

@dataclass
class AppStateVars:
app: JrtcApp
agg_cnt: int
received_counter: int

##########################################################################
# Handler callback function (this function gets called by the C library)
def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(struct_jrtc_router_data_entry), state_ptr: int):
def app_handler(timeout: bool, stream_idx: int, data_entry: struct_jrtc_router_data_entry, state: AppStateVars):

GENERATOR_OUT_STREAM_IDX = 0
SIMPLE_INPUT_IN_STREAM_IDX = 1
Expand All @@ -43,10 +41,6 @@ def app_handler(timeout: bool, stream_idx: int, data_entry_ptr: ctypes.POINTER(s

else:

# Dereference the pointer arguments
state = ctypes.cast(state_ptr, ctypes.POINTER(AppStateVars)).contents
data_entry = data_entry_ptr.contents

if stream_idx == GENERATOR_OUT_STREAM_IDX:

state.received_counter += 1
Expand Down Expand Up @@ -112,7 +106,7 @@ def jrtc_start_app(capsule):
)

# Initialize the app
state = AppStateVars(agg_cnt=0, received_counter=0)
state = AppStateVars(agg_cnt=0, received_counter=0, app=None)
state.app = jrtc_app_create(capsule, app_cfg, app_handler, state)

# run the app - This is blocking until the app exists
Expand Down
2 changes: 1 addition & 1 deletion src/pythonapp_loader/jrtc_pythonapp_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ jrtc_start_app(void* args)
Py_XDECREF(pCapsule);

cleanup_gil:
PyGILState_Release(gstate);

if (ts1) {
if (main_ts != ts1) {
Expand All @@ -371,6 +370,7 @@ jrtc_start_app(void* args)
PyThreadState_Swap(main_ts);
Py_EndInterpreter(ts1);
}
PyGILState_Release(gstate);

if (Py_IsInitialized()) {
Py_Finalize();
Expand Down
Loading
Loading