Wardruna
Wardruna

Reputation: 308

How to run a function without interrupting program in python?

I am new at python and i work on "Finding heart beat from webcam" project and i want results(pulses) to be added to csv file in every second.You can see base project here :Base project link

In get_pulse.py i made few changes.In summary this is my function:

from lib.device import Camera
from lib.processors import findFaceGetPulse
from lib.interface import plotXY, imshow, waitKey,destroyWindow, moveWindow
import numpy as np      
import datetime
import csv
import time
import threading


     def add_csv(self):
            """
            Adds current data to a csv file
            """
            threading.Timer(1.0,add_csv).start()

            bpm = " " + str(int(self.processor.measure_heart.bpm))

            fd = open('msp.csv','a')
            fd.write(bpm)           
            fd.close()

When i run add_csv function i get this error:

Traceback (most recent call last):
  File "get_pulse.py", line 169, in <module>
    App.main_loop()
  File "get_pulse.py", line 164, in main_loop
    self.key_handler()
  File "get_pulse.py", line 135, in key_handler
    self.key_controls[key]()
  File "get_pulse.py", line 65, in add_csv
    threading.Timer(1.0,add_csv).start()
NameError: global name 'add_csv' is not defined     

So,how can i append this data to a file without interrupting program?

Upvotes: 0

Views: 253

Answers (1)

User
User

Reputation: 14873

use

threading.Timer(1.0,self.add_csv).start()

Upvotes: 1

Related Questions