Repository navigation
Conversation
When the REPL is waiting for user input, it is blocking-read from stdin. Micropython event on the same thread would have to wait for the user to interact This change prevents read from blocking by first making sure there is something to read, otherwise it polls and handles Micropython pending events (mp_handle_pending) This is in line with other port\'s REPLs such as ESP32, which do not block
| } | ||
| #endif | ||
|
|
||
| static bool inputAvailable() |
There was a problem hiding this comment.
Maybe this should be inputAvailable(void) to be consistent with other functions?
There was a problem hiding this comment.
Yes, and also input_available(void), please check the code style from the surrounding functions. Also for the rest: opening brace on same line, spaces around +
| FD_ZERO(&fds); | ||
| FD_SET(STDIN_FILENO, &fds); | ||
| select(STDIN_FILENO+1, &fds, NULL, NULL, &tv); | ||
| return (FD_ISSET(0, &fds)); |
There was a problem hiding this comment.
Not sure the the extra brackets are needed?
|
@embeddedt @stinos Fixed according to your comments. |
@embeddedt Just missed that. Fixed now. |
|
You still missed the opening brace for the while loop :) |
Are you able to provide an example of such an event? An event like a timer expiry would trigger an asynchronous signal which can execute even during the blocking read. And such a signal should also make the read call terminate with EINTR. So it'd be good to see an example where the blocking read actual blocks the events. |
@dpgeorge - Any event that schedules uPy, by calling Specifically, I'm working on LittlevGL Micropython Bindings. It's designed to be single threaded, but lvgl requires periodic calls to On embedded architectures a timer interrupt schedules Without this fix, the GUI would "freeze" during blocking read from stdin. |
You can use unix signals to execute a callback asynchronously. See micropython-lib/machine/machine/timer.py for code that does that (in Python). Even at the blocking REPL it will still execute a signal callback. The deeper issue here is power consumption and the ability to idle when there is nothing going on. If MicroPython is responsible for running an event loop on idle then there's not much of a chance to sleep. Eg in an embedded system it should be possible to sleep the MCU until an event (IRQ) arrives, at which point it can be processed, then go back to sleep. For things that require continual work (like a graphics system) there can be a timer that fires an event at regular intervals, but at least it's still event based, not polling based. You can see the above problem with the PR here: when the unix executable is running at an idle prompt it now has about 10-15% CPU load, compared to 0% before this PR. |
| #endif | ||
| while (!input_available()) { | ||
| mp_handle_pending(); | ||
| usleep(1); |
There was a problem hiding this comment.
The problems @dpgeorge has mentioned would effectively disappear if we slept for 1 or 10 milliseconds instead of 1 microsecond.
There was a problem hiding this comment.
@embeddedt I would first like to try his suggestion executing uPy asynchronously without calling mp_sched_schedule.
@dpgeorge The problem is that there is no uPy thread context when I call the asynchronous function. So I copied some code from typedef struct _thread_data_t
{
size_t stack_size;
mp_obj_dict_t *locals;
mp_obj_dict_t *globals;
} thread_data_t;
STATIC int thread_entry(void *_data) {
thread_data_t *data = _data;
// Execution begins here for a new thread. We do not have the GIL.
mp_state_thread_t ts;
mp_thread_set_state(&ts);
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
mp_stack_set_limit(data->stack_size);
// set locals and globals from the calling context
mp_locals_set(data->locals);
mp_globals_set(data->globals);
MP_THREAD_GIL_ENTER();
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
tick_thread(NULL);
nlr_pop();
} else {
// uncaught exception
// check for SystemExit
mp_obj_base_t *exc = (mp_obj_base_t*)nlr.ret_val;
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
// swallow exception silently
} else {
// print exception out
mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in SDL thread!\n");
mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(exc));
}
}
MP_THREAD_GIL_EXIT();
return 0;
}
...
...
{
static thread_data_t thread_data;
thread_data = (thread_data_t){SDL_THREAD_STACK_SIZE, mp_state_ctx.thread.dict_locals, mp_state_ctx.thread.dict_globals};
SDL_CreateThread(thread_entry, "tick", &thread_data);
}Very verbose and far from the one-liner I had before, when using For the record, I still think it's worth allowing polling, even optionally, to make things simpler. Closing. |
I agree. On a modern processor, the load of waking up every 10ms should not be of concern, in my opinion. |
I think the way to do it efficiently would be to poll on stdin with no timeout (ie forever) in a loop until data is ready, and in that loop call |
@dpgeorge The GUI needs to update every few milliseconds. Do you suggest we raise a signal every few milliseconds, to interrupt the poll? Isn't that intrusive? Suppose the user code is waiting for some signal he is generating. He doesn't expect a signal to come from someplace else (unless this is "SIGTERM" or something that can be expected from the OS). |
|
Plus, IMO, raising a signal every 10ms and sleeping for 10ms take up roughly the same CPU time. |
That does sound like what he's suggesting. And at some level, that's pretty much how sleep works anyway. I think the main point is that the signal and the interval becomes completely up to your program or use case. It doesn't matter which signal you use, and you don't even have to handle it anywhere, as long as it results in You could use timer_create() to make a periodic timer with a signal number and interval of your choice, and only when littlevgl is running -- but importantly this means that the behaviour is unchanged for everyone else. (And timer_create() is exactly what machine/machine/timer.py uses, so you can do all this from Python if you choose).
Maybe, but they're vastly different in terms of program behaviour. Sleeping for 10ms means you now have a per-byte latency of up to 10ms. Probably not a big deal for the repl, but the point above is that the sleep implementation would mean that all users of the unix port now get this, whereas the signal is only raised if you choose to (i.e. because your program needs it). And remember the unix port can be used on quite a wide range of devices, not just modern PC CPUs. Just throwing it out there though, a third alternative: I notice that the way input_available() in this PR works is to do a non-blocking |
|
I agree with @jimmo 's comments above. The way to do it efficiently is to only raise a signal (and hence EINTR the select/poll/read) when needed. Eg if |
@jimmo littlevgl is a library, not the application itself. My point is that raising a signal might be intrusive towards a user application. That user application might not expect us to send this signal, and it would affect its flow. What you suggest would require any application that uses littlevgl to be aware that signals are raised. For example, if someone calls "read" in his application, he would need to take into account a possible EINTR result. (btw, using a separate thread for the GUI event loop can prevent this, but introduces thread safety issues) |
|
For EINTR see related PEP 475 https://www.python.org/dev/peps/pep-0475/ and #3268 |
Ok. I tried your suggestion to raise a signal. It didn't go so smooth.
So it works now with Another annoyance: When running with gdb, it stops by default on @dpgeorge, @jimmo - Are you still convinced that using signal is the right way to do this? |
|
@amirgon Yes, it does matter which signal you choose; some of them just terminate the process (ab)normally. https://pubs.opengroup.org/onlinepubs/009695399/basedefs/signal.h.html |
Yes I was referring to @jimmo's claim that "it doesn't matter which signal you use", but I think both him and me took for granted that we shouldn't use a signal that terminates the process... My point is that even other "innocent" signals create some problems. |
|
If it can be documented that certain signals are reserved for MicroPython/lv_micropython (i.e. |
Added SIGUSR2 and SIGALRM sigaction, to prevent them from killing the application which is the default behavior
…o unix_repl_fix
…nto unix_repl_fix
The user would need to call sigaction in user code. Calling sigaction is required, to prevent signals default behavior of terminating the application. sigaction can be called from either C or Python code with ffi. See https://github.com/micropython/micropython-lib/tree/master/signal
|
@dpgeorge Is anything still wrong or missing? After my last commit, a month ago, the change is very small and straightforward: int ret;
do {
mp_handle_pending();
ret = read(0, &c, 1);
} while (ret == -1 && errno == EINTR);(I removed the calls to |
|
Alternate implementation in #5723 |
|
#5723 is a good solution for this problem. Special considerations when using this:
|
Remove mp_instance_cast_to_native_base and MP_ERROR_TEXT defines from lv_conf.h small fixes in gen_mpy.py SDL: use signals instead of polling when waiting to user input, see micropython/micropython#4953 (comment) Advanced demo: improve gradient to work better with ili9341
Remove mp_instance_cast_to_native_base and MP_ERROR_TEXT defines from lv_conf.h small fixes in gen_mpy.py SDL: use signals instead of polling when waiting to user input, see micropython/micropython#4953 (comment) Advanced demo: improve gradient to work better with ili9341
…n-main Translations update from Weblate
When the REPL is waiting for user input, it is blocking-read from stdin. Micropython event on the same thread would have to wait for the user to interact
This change prevents read from blocking by first making sure there is something to read, otherwise it polls and handles Micropython pending events (mp_handle_pending)
This is in line with other port's REPLs such as ESP32, which do not block