Python Forum
Inconsistent counting / timing with threading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistent counting / timing with threading
#1
I have made a counter using threading I'm not sure why when the tick thread is flipping between 1 and 0 it is not consistent with its timing. Also with my incremental counter counting the changes of the other thread it changes the value in clumps.

import threading
import time

countval = 0
def tick():
    global countval
    while True:
        countval = 1
        time.sleep(1)
        countval = 0
        time.sleep(1)
        print ("clock value: " + str(countval))

def increment_value():
    global countval
    plus = 0
    flip = 1
    while True:
        if countval == flip:
            plus = plus + 1
            flip = 0
            print ("count up value: " + str(plus))
        if countval == 0:
            flip = 1

incr_thread = threading.Thread(target=increment_value)
tick = threading.Thread(target=tick)
incr_thread.start()
tick.start()
Here's the output I get what would cause it to be so inconsistent?
I would expect it to output count up value then clock value and so on but it sometimes puts 2 count up values.


Quote:count up value: 1
clock value: 0
count up value: 2
count up value: 3
clock value: 0
count up value: 4
count up value: 5
clock value: 0
count up value: 6
count up value: 7
clock value: 0
count up value: 8
clock value: 0
count up value: 9
clock value: 0
count up value: 10
count up value: 11
clock value: 0
count up value: 12
clock value: 0
count up value: 13
count up value: 14
clock value: 0
count up value: 15
count up value: 16
Reply
#2
I would expect something like this:
import threading
import time
 
toggle = 0

def tick():
    global toggle
    while True:
        time.sleep(1)
        toggle = 0 if toggle == 1 else 1
        print ("toggle:", toggle)
 
def increment_value():
    last = toggle
    count = 0
    while True:
        if last != toggle:
            last = 0 if last == 1 else 1
            count += 1
            print ("count:", count)
 
incr_thread = threading.Thread(target=increment_value)
tick = threading.Thread(target=tick)
incr_thread.start()
tick.start()
As far as increment_value() is concerned, countval can change at any time. Your code behaves differently depending on where it is in increment_value() when countval changes.
def increment_value():
    global countval
    plus = 0
    flip = 1
    while True:
        if countval == flip:  # If countval changes 1->0 here, flip == countval so plus is incremented
            plus = plus + 1
            flip = 0
            print ("count up value: " + str(plus))
        if countval == 0:   # If countval changes 1->0 here, flip is set to 1 and we don't count the tick
            flip = 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,836 May-03-2023, 08:22 AM
Last Post: billykid999
  Read csv file with inconsistent delimiter gracenz 2 1,201 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Inconsistent loop iteration behavior JonWayn 2 1,002 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,476 Jun-16-2022, 06:59 PM
Last Post: saoko
Question Timing actions with Python dangermaus33 0 1,011 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Loop Dict with inconsistent Keys Personne 1 1,610 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,127 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Inconsistent behaviour in output - web scraping Steve 6 2,550 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,899 Sep-15-2021, 12:48 PM
Last Post: jenya56
  Packages inconsistent warning during hdbscan install Led_Zeppelin 0 1,930 Aug-31-2021, 04:10 PM
Last Post: Led_Zeppelin

Forum Jump:

User Panel Messages

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