Callback or event signalling from c to python

Hi there! Are there any examples you may share on the following scenarios?

  1. Calling a python function/sub from a C program?
  2. Sincronizing C and python by means of an “Event”

What I’m looking into is to reuse the http_server example from espressif and somehow “calling” python when we receive a valid request.

Regards
Alex

Hi @Alex1
Currently, it is therefore possible to call C functions from Python, but not (yet) Python functions from C.
Check this doc for more info on C language interface.
However, I think there is a workaround until this feature is implemented. (But not a clean approach :sweat:)
You can have a C function that checks for event, A python function that calls this C function and checks if a flag has been raised. and a python thread that uses this python function.

@native_c("_get_event",["HTTPserver_c_stuff.c"],[])
def _get_event():
    pass

def _event_loop():
    try:
        while True:
            evt_type = _get_event()
            if evt_type == 0: # EVENT
                #do Stuff
           
    except Exception as e:
        pass

def start():
    thread(_event_loop)

Let me know if you are developing a commercial product, If so, this feature could be arranged with the Zerynth team.

Hi @karimhamdy1, sorry for the late reply.
Yes I’m developing a commercial product.

With regard to this thread I decide to go classic python only and work with the timeouts until Zerynth gives us callbacks for sockets, I just started a thread to handle all sockets comms and seem to work fine.

Thanks any way.

Best regards
Alex

1 Like