Page 1 of 1

How to soft reset the whole context of upy when process running?

Posted: Sat Nov 09, 2019 3:21 am
by tim3385
THIS TOPIC IS MOVED FROM ISSUES OF GITHUB(Please remove the topic that I post in "General Discussion and Questions"):

I integrated upy into my project(Thanks for your great work). upy works well in my project process.
In one running process, the python script file(A in short) could be reloaded when it was modified. But another python script file(B in short) that imported by A could NOT be reloaded when it was modified. Therefore I want to reset "the contex" of upy.

I have tried to use mp_deinit and then re-initialize upy as the same as I do when the process startup. But when I invoke a python script again, MP_STACK_CHECK(mp_stack_check) will fail.

Thank you.

dpgeorge REPLY ME AT GITHUB ISSUES:
dpgeorge:
This type of question is better suited to https://forum.micropython.org

A short answer: there are lots of examples of how to do a "soft reset" in the bare-metal ports, eg stm32, esp8266, esp32, nrf.

Re: How to soft reset the whole context of upy when process running?

Posted: Sat Nov 09, 2019 3:21 am
by tim3385
This is my code refer from some ports implementation. upy works but soft-reset not works yet.

Code: Select all


#if MICROPY_ENABLE_GC
long heap_size = 1024*1024 * (sizeof(mp_uint_t) / 4);
#endif

static char *heap = NULL;

void __wxpyInit(void) {
#if MICROPY_PY_THREAD
	mp_thread_init();
#endif
	mp_stack_ctrl_init();
	static char *stack_top;
	int stack_dummy;
	stack_top = (char*)&stack_dummy;
	mp_stack_set_top(stack_top);

	mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
#if MICROPY_ENABLE_GC
	heap = malloc(heap_size);
	gc_init(heap, heap + heap_size);
#endif
#if MICROPY_ENABLE_PYSTACK
	static mp_obj_t pystack[1024];
	mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]);
#endif
	mp_init();

	const char* path = "~/.micropython/lib:/usr/lib/micropython";
	size_t path_num = 1; // [0] is for current dir (or base dir of the script)
	if (*path == ':') {
		path_num++;
	}
	for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
		path_num++;
		if (p != NULL) {
			p++;
		}
	}
	mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), path_num);
}

void __wxpyFinalize() {
	const int NOTHING_EXECUTED = -2;
	int ret = NOTHING_EXECUTED;
	bool inspect = false;
#if MICROPY_PY_MICROPYTHON_MEM_INFO
    if (mp_verbose_flag) {
        mp_micropython_mem_info(0, NULL);
    }
#endif
#if MICROPY_PY_THREAD
	mp_thread_deinit();
#endif
#if defined(MICROPY_UNIX_COVERAGE)
	gc_sweep_all();
#endif
    mp_deinit();
#if MICROPY_ENABLE_GC && !defined(NDEBUG)
    free(heap);
#endif
}



Re: How to soft reset the whole context of upy when process running?

Posted: Fri Nov 22, 2019 4:12 am
by Damien
In your code you call both mp_stack_ctrl_init() and mp_stack_set_top(...). Only one of these should be called, usually it's mp_stack_ctrl_init() which sets the stack top automatically. mp_stack_set_top is only used if setting it automatically doesn't work.

Otherwise, the stack limit checking is quite dependent on the architecture and compiler. If the compiler inlines a lot of code then the stack check might not work as expected. If you target Javascript then the stack check won't work because the Javascript emulated C stack grows up, not down.