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

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
tim3385
Posts: 8
Joined: Fri Mar 22, 2019 11:14 am

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

Post by tim3385 » Sat Nov 09, 2019 3:21 am

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.
Last edited by tim3385 on Sat Nov 09, 2019 3:22 am, edited 1 time in total.

tim3385
Posts: 8
Joined: Fri Mar 22, 2019 11:14 am

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

Post by tim3385 » Sat Nov 09, 2019 3:21 am

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
}



Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

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

Post by Damien » Fri Nov 22, 2019 4:12 am

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.

tim3385
Posts: 8
Joined: Fri Mar 22, 2019 11:14 am

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

Post by tim3385 » Wed Dec 18, 2019 9:47 am

At the beginning, there was no mp_stack_set_top(...). I just tried it after I encounter this problem.

Now I just in ports/windows/mpconfigport.h(javascript port has no MICROPY_STACK_CHECK define originally)
#define MICROPY_STACK_CHECK (0)
Hope there no other problems~~

ps. My applicataion need run in Windows/WASM, and Android/iOS/macOS/Linux/... later.

BTW. I post another issue:
https://github.com/micropython/micropython/issues/5434

Thank you.

Post Reply