dfieni3
Posts: 17
Joined: Mon Jun 10, 2013 4:53 pm

Continuous loop using threading

Fri Jul 26, 2013 12:06 pm

Hi, I am somewhat new to python. I have been trying to find the answer to this coding question for some time. I have a function set up to run on a threading timer. This allows it to execute every second while my other code is running. I would like this function to simply execute continuously, that is every time it is done executing it starts over, rather than on a timer. The reason for this is that due to a changing delay in a stepper motor the function takes different amounts of time run.

User avatar
paddyg
Posts: 2461
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Continuous loop using threading

Fri Jul 26, 2013 9:09 pm

I'm sure if you post the relevant code someone will help. But, have you tried simply reducing the time in your timer?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

jakebpg
Posts: 36
Joined: Tue Sep 04, 2012 9:36 pm

Re: Continuous loop using threading

Sun Jul 28, 2013 11:42 pm

Here is how you can do it with a separate thread.
Not sure if this is what you're looking for but it will give you an idea of how to do it.

Code: Select all

#!/usr/bin/python3

import sys, time
from PyQt4 import QtCore, QtGui 
from os.path import join
from datetime import datetime	#used for the date/time in the thread

class MainForm(QtGui.QMainWindow):

	def __init__(self):
		super(MainForm, self).__init__()
		
		self.initUI()
		
        
	def initUI(self):
		
		# Create a set of lcds to display the temps
		self.lcdin = QtGui.QLCDNumber(self)
		self.lcdin.move(20, 30)
		
		# create information labels 
		
		self.lbltime = QtGui.QLabel('Time', self)
		self.lbltime.move(28, 4)
		
		#Start Button
		self.startButton = QtGui.QPushButton("&Start", self)
		self.startButton.resize(self.startButton.sizeHint())
		self.startButton.move(60, 120)
		self.startButton.clicked.connect(self.start_Updating)
		
		# Resize the form & show it center screen
		self.resize(280, 150)
		self.center()
		self.setWindowTitle('Current Time')    
		self.show()
		self.start_Updating()
		
	def center(self):
		qr = self.frameGeometry()
		cp = QtGui.QDesktopWidget().availableGeometry().center()
		qr.moveCenter(cp)
		self.move(qr.topLeft())
		
		
	def start_Updating(self):
		
		self.startButton.setEnabled(False)
		self.threads = []
		updater=UpdateForm(self.lcdin, self.lbltime)
		self.threads.append(updater)
		updater.start()	
		return 
		
		
class UpdateForm(QtCore.QThread):
	
	def __init__(self, lcdin, lbltime):
		QtCore.QThread.__init__(self)
		#self.n = n
		self.lcdin = lcdin
		self.lbltime = lbltime

	def run(self):
		self.upsdate(1)
		
	def upsdate(self, n):
		while True:
			print('Hello RPI...')
			print(datetime.now().strftime('%-I:%M:%S,%p %-m-%-d-%Y '))
			clock=datetime.now().strftime("%-I:%M:%S")
			print(clock)
			self.lcdin.display(clock)
			time.sleep(n)


def main():
	
	app = QtGui.QApplication(sys.argv)
	ex = MainForm()
	sys.exit(app.exec_())


if __name__ == '__main__':
	
	main() 

Now I use geany for coding and it works in geany. It should also work if you just copy and paste to a file and give it the .py extension and make the file executable.

Hope this helps and I hope the copy/paste didn't mess up the indents to bad..

Let me know.

User avatar
peachoftree
Posts: 4
Joined: Mon Aug 05, 2013 2:48 am

Re: Continuous loop using threading

Tue Aug 06, 2013 2:38 pm

call your function inside of a while loop.

jakebpg
Posts: 36
Joined: Tue Sep 04, 2012 9:36 pm

Re: Continuous loop using threading

Tue Aug 06, 2013 5:16 pm

Peach, a while loop most likely wont work for what he is doing since it will lock the program from all other input while it is in the loop.
He would have to break the loop preform some task restart the loop and break it again to preform another task.
A separate thread to preform the counting of a timer our simply an interval of time as shown in my code snippet is probably the best way to go since there is no worrying about having to break a while loop and restart it constantly.

Return to “Python”