1

I want to repeat a function very 2 minutes on a list and move every time one element further of the list. I use the 'schedule' to do the task. The script is the following:

if __name__ == '__main__':
    machines_queue=[[...],[...],...] # list of the sublists
    global counter
    counter=   0

    def job(sub_list):
        somefunction(sub_list) # here I want each 2 min  iteration a sublist from machine_queue further  
        counter= counter +1    # here is pop the Error on the counter  
        print("I'm working...")

    schedule.every(1).minutes.do(job,sub_list= machines_queue[counter])
    print(counter)


    while True :
        schedule.run_pending()` 

The first time it goes well , them I get an on the counter this Error:

"UnboundLocalError: local variable 'counter' referenced before assignment."

I don't understand because I set before the variable as global so I don't get the scope issue.

If you have an idea please .... :) Thank you in advance for your help

Remi-007
  • 145
  • 1
  • 11
  • Possible duplicate of [Python: UnboundLocalError: local variable 'count' referenced before assignment](https://stackoverflow.com/questions/36720607/python-unboundlocalerror-local-variable-count-referenced-before-assignment) – Isaac Aug 16 '18 at 16:28

2 Answers2

1

You should use the global statement inside the function you want to allow access to the variable. Move global counter to the beginning of the job function and it would work.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    Just figured it out then I saw you post! glad I was right though!:P – Isaac Aug 16 '18 at 16:28
  • Thank you :) I agree it makes sense, but then I got then the problem further at schedule.every(1).minutes.do(job,sub_list= machines_queue[counter]) The Error is : 'NameError: name 'counter' is not defined' . – Remi-007 Aug 16 '18 at 16:48
0

My working solution to do a task every to minutes with various functions and conditions in this task: just do a normal loop as usual and add a time sleep at the end, ( ex: time.sleep(120) .Then wrap the all code in a function / main and use Schedule library on it if you still need it ....

Remi-007
  • 145
  • 1
  • 11