_thread | NameError #15686
Replies: 3 comments
-
Would be a good idea to put data which is accessible from both running cores into a global array. a=0
print(id(a))
a +=1
print(id(a)) arrays will stay in memory for their entire lifetime. So, I'd try float_var = array.array('f', (3.14159,))
print(id(float_var), float_var[0])
float_var[0] += 1.0
print(id(float_var), float_var[0]) Using that technique AND a proper locking mechanism, I had never any problems using two threads communicating. |
Beta Was this translation helpful? Give feedback.
-
As a very general comment, threaded code is an advanced technique which is best deployed when other, simpler approaches, are inadequate. The best way to achieve reliable concurrency in most applications is with |
Beta Was this translation helpful? Give feedback.
-
Please format posted code with backticks, see #9111 Use asyncio instead of threads, as explained in the previous post. Initialize global variables in the global scope instead of inside the Watchdog function. If main() runs before Watchdog() has gone through the global variable initialization, there will be a NameError. On other words: move the global variable initialization to be done before the |
Beta Was this translation helpful? Give feedback.
-
Hi!
I'm writing a very simple program in MicroPython to run on a Raspberry Pi Pico. I'm using MicroPython version 1.23.0 and I wrote a function to monitor the microcontroller's GPIO inputs using Python's _thread mode to support multithreading.
However, I noticed that the task placed in the microcontroller's core sometimes works correctly and when it doesn't, it is interrupted and displays the following message on the console:
Unhandled exception in thread started by <function Watchdog at 0x20007860>
Traceback (most recent call last):
File "", line 171, in Watchdog 26.85357NameError
: name 'Remoto' isn't defined
The global variable "Remoto" was declared at the beginning of the "Watchdog" function and its initial value is "False". This variable is also declared and initialized in the main program body.
I must be making some basic syntax error since I recently started programming in Python. I ask for everyone's help to solve this problem.
Below is the script for the Watchdog function:
import time
from machine import Pin
from machine import ADC
import _thread
#--------------------------- Watchdog --------------------------------#
Watchdog
This function operates on the second core of the processor and is intended to
to monitor the status of the control panel inputs and outputs
def Watchdog():
_thread.start_new_thread(Watchdog,())
Beta Was this translation helpful? Give feedback.
All reactions