Understanding Inter-Thread Communication with Low-Level API_thread in micropython

Asked 2 years ago, Updated 2 years ago, 20 views

I am running micropython on HW called maixduino in sipeed, but it supports a low level of _thread API, and I would like to communicate data between threads. What should I do?

abc=0

testThread():
    cnt = 0
    while True:
        time.sleep_ms(100)
        lcd.draw_string(0,20, "cnt1:" + str(cnt))
        cnt+=1
        abc+=1

The variables declared in global as described above did not appear to be visible.

python

2022-09-29 22:05

1 Answers

With this much source, regardless of micro or thread, is it a variable scope issue?
Like this article.
Using a global variable with a thread

You don't need to just look at it, but if you want to update it to have other effects, you'll need a declaration that uses the global variable at the beginning of the function.

abc=0

testThread():
    global abc# global variable declaration
    cnt = 0
    while True:
        time.sleep_ms(100)
        lcd.draw_string(0,20, "cnt1:" + str(cnt))
        cnt+=1
        abc+=1

Also, if it is in the same file, the above is fine, but there may be situations where ingenuity is required.
Global Variables

How to Declare global variables in micropython that can be accessible from boot.py, main.py and many files.
creating and writing and updating file data taking time and getting into error. I want to detail only 2 variables.
Also want to create and freeze (compile) code for python server that listen on port for request.

How to declare global variables in micropython, which is accessible from boot.py, main.py, and many files.
Creating, writing, and updating file data takes time and results in errors.Declare only two variables.
You also want to create and freeze (compile) the Python server code to listen to requests on the port.

Depending on your variable you can do as @stijn Suggested in this post.

Depending on the variable, you can run it like @stijn suggested in this post.

If you create a variable in boot.py it will be available to main.py and to the REPL. But it won't be available to modules you import because of the way Python names work: you would need explicitly to pass to functions / methods in the module.

When you create a variable on boot.py, it becomes available on main.py and REPL. However, it is not available on the module that you import because of the way Python namespace works.It must be explicitly passed to the functions/methods in the module.

stijn-Re: Is there an easy way to export the value of a #define from mpconfigport.h to Python code?

Something like this in main.c, after initialization and before the REPL or file execution:

Something like main.c, after initialization, before REPL or file execution:

mp_store_global(QSTR_FROM_STR_STATIC("dupterm_value"), mp_obj_new_int(MICROPY_PY_OS_DUPTERM));

when you enter the REPL or in a file, dupterm_value is in the global scope so you can access it.g.

Then you can enter it in REPL or in a file to access it because dupterm_value is in the global scope.


2022-09-29 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.