Repository navigation
Thread does not handle import correct #19682
Description
Activity
Tip: use 'triple backtick'
```py
# code goes here
```around your code snippets to get syntax highlighting and proper indenting.You can edit the post above
In some forums you could change the
divelement topreto get the whitespace back. But this doesn't work on GitHub. AFIK, whitespace at the beginning are stripped away, if you don't put your code in code tagsSorry for the editing.
Reacted by Jos VerlindeSorry for the editing.
That's what the feature is for :-)
For example, I've now removed the code so it doesn't appear twice. But you can still see the history.
Reacted by Jos VerlindeTip: use 'triple backtick' [...] around your code snippets [...].
Maybe this reminder could be added to the templates in
.github/ISSUE_TEMPLATE?Reacted by Jos VerlindeI pasted this code on an RP2350 and it worked:
MicroPython v1.28.0 on 2026-04-06; Raspberry Pi Pico2 with RP2350 Type "help()" for more information. >>> paste mode; Ctrl-C to cancel, Ctrl-D to finish === import time === from machine import mem32 === import _thread === === def thread_entry(a, b): === try: === core_num = mem32[0xd0000000] === except Exception as e: === print(f'function thread entry exception {e}') === print(f'core {core_num} thread {_thread.get_ident()} {a} {b}') === === # spawn thread using kw args === id = _thread.start_new_thread(thread_entry, (10, 20)) === core_num = mem32[0xd0000000] === print(f'core {core_num} thread {_thread.get_ident()}') === === time.sleep(1) === core 0 thread 1 core 1 thread 2 10 20 >>>
[EDIT]
I repeated this test on an RP2040 running firmware V1.29 with the following outcome:- Pasted, the code ran correctly.
- Saving the code to the flash as
rats.pyand issuingimport ratsat the REPL the error occurred. - With an
importstatement inside thethread_entryfunction (as below) it ran correctly.
def thread_entry(a, b): from machine import mem32 # Rest of code as above
Port, board and/or hardware
Pico
MicroPython version
MicroPython v1.30.0-preview.58.g14e9793c91 on 2026-09-04; Raspberry Pi Pico with RP2040
Reproduction
Expected behaviour
Expected no exception. See different behavior based on the order of execution.
Observed behaviour
core 0 thread 1
function thread entry exception name 'mem32' isn't defined
Unhandled exception in thread started by <function thread_entry at 0x20007250>
Traceback (most recent call last):
File "", line 13, in thread_entry
NameError: local variable referenced before assignment
Additional Information
In the code posted above, in the function 'thread_entry' changing mem32 to machine.mem32 resolves the issue.
Alternately, moving 'core_num = mem32[0xd0000000]' to before starting the thread resolves the issue.
Code of Conduct
Yes, I agree