Page 1 of 1

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

Posted: Thu Nov 07, 2019 4:19 pm
by tim3385
THIS TOPIC IS MOVED FROM ISSUES OF GITHUB:

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: Thu Nov 07, 2019 4:23 pm
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
}