Question

I am working on some code that will count mouse clicks on a beaglebone board in python.

My question is how to structure code such that I can access the total number of mouse clicks while the mouse click counter function is still running (indefinately), and at the same time not interrupt the mouse click counter function ( i dont want to miss a click!)?

Is there a way to access variables in a running function in python without interrupting it?

Was it helpful?

Solution

Depends on how you're wanting to access them. An infinite loop works well.

_clicks = 0
while True:
    if _clicks != clicks:
        _clicks = clicks
        print(_clicks)

But you'll almost certainly have to put that in another thread.

from threading import Thread

def notifier():
    global clicks # whatever variable you're accessing
    while True:
        if _clicks != clicks:
            _clicks = clicks
            print(_clicks)

t = Thread(target=notifier)
t.start()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top