Python Forum
Function global not readable by 'main'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function global not readable by 'main'
#1
Wierd problem been trying to resolve for several hours - appreciate any help !

I'm runinng an ADC-sampler in a separate thread function, which logs the max-sample found during each second (approx).

It signals 'main' that the max-value is ready via a flag global.... while also posting the max-value in a data global.

Below is the code and the output. 'main' gets signaled properly via the flag global, but the data-value doesn't arrive - it's usually 0, but about 20% of the time some odd non-0 value.

import machine
import _thread

################################################
# Variables
global readyFlag	
readyFlag = False

global max_found
max_found = 0

################################################
# Functions

def sampler_thread(): 		# Function that will be launched as a separate thread.
    global readyFlag
    global max_found
    
    adc1 = machine.ADC(26)            # Start an ADC
    while True: 
        max_found = 0                 # Init to smallest possible value before sampling-loop

        cnt = 20000                   # Init loop-cntr
        while cnt > 0:                # Loop until counter=0
            cnt -= 1
            sample = adc1.read_u16()  # Get next sample
            if sample > max_found:    # If >max-found, set it to max-found
                max_found = sample

        print("sampler max_found:", max_found)
        readyFlag = True              # Tell the 'main' thread that max_found is ready.

##############################################
# Main 
_thread.start_new_thread(sampler_thread, ())

while True:
    if readyFlag:
        print("main max_found:",max_found)
        readyFlag = False
**********************
OUTPUT SAMPLE ('sampler max_found' is correct, but 'main' gets mostly 0's or odd#):
sampler max_found: 2688
main max_found: 2368

sampler max_found: 2704
main max_found: 0

sampler max_found: 2672
main max_found: 0

sampler max_found: 2688
main max_found: 2128
Reply
#2
What is the target os? Why aren't you using the threading module?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  io.UnsupportedOperation: not readable RedSkeleton007 2 18,784 Nov-06-2023, 06:32 AM
Last Post: gpurdy
  How to make x-axis readable with matplotlib Mark17 7 3,941 Mar-01-2022, 04:30 PM
Last Post: DPaul
  Finding global extrema of oscillating function JoeRogan 0 1,659 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  How to make global list inside function CHANKC 6 3,123 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,236 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  how to run another function from main file Mekala 3 2,570 Aug-09-2020, 04:41 AM
Last Post: deanhystad
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,254 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Problem with user defined main menu function stefzeer 3 2,413 Mar-27-2020, 06:12 AM
Last Post: buran
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 3,049 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  io.UnsupportedOperation: not readable navidmo 1 3,535 Oct-31-2019, 11:04 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020