Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2013, 03:12 AM
yo9hnf yo9hnf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 yo9hnf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 30 sec
Reputation Power: 0
Run a function for X seconds

Hello,

My name is Radu and I am from Romania.

I have a problem with a coundown routine.
My program basicaly looks like this :

main():
...something...
while True :
if Button1_pressed....
If Button2_pressed...
If PLAY pressed...
if NEXT pressed...
etc

So, when i press the PLAY button i need to run a function for a specific time (like 10 seconds), BUT in this time a may press NEXT, so i can't use time.sleep(seconds).

How can i make this run ?

Thank you !

Regards,
Radu

Reply With Quote
  #2  
Old January 30th, 2013, 09:03 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
run the function in a separate thread or process.
Then kill that process on appropriate event.
One of the events is the 10 second timer,
Various buttons could preempt the timer.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 30th, 2013, 11:06 AM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 315 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 23 h 29 m 31 sec
Reputation Power: 7
Assuming you are using Wx for the GUI, use timer() (many examples on the web) to call some function that will set a variable causing the function to exit. Note that Wx also has threads.

Reply With Quote
  #4  
Old January 30th, 2013, 11:09 AM
yo9hnf yo9hnf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 yo9hnf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 30 sec
Reputation Power: 0
Thank you for your replays !

I dont't have a GUI ....I have a 2x16 LCD.
The buttons are (hardware) push-buttons.

I can't figure out the algorithm

Reply With Quote
  #5  
Old January 30th, 2013, 02:21 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 315 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 23 h 29 m 31 sec
Reputation Power: 7
Then multiprocessing/threading is the only option since you want to do 2 things at the same time, keep track of the time, and check for button presses. A simple example where the process is terminated after 5 seconds and the list's value is changed to simulate a button press or whatever, which could also return/exit from the function.
Code:
import time
from multiprocessing import Process

class TestClass():

   def __init__(self):
      self.ctr = 0
      self.ctr_list=[0]

   def test_f(self, name):
      while True:
         self.ctr += 1
         print "test_f", self.ctr, name, self.ctr_list
         time.sleep(0.5)

if __name__ == '__main__':
   CT=TestClass()
   p = Process(target=CT.test_f, args=('P',))
   p.start()

   time.sleep(2)
   CT.ctr_list[0]=10
   print "ctr_list", CT.ctr_list

   ## sleep for 5 seconds and terminate
   time.sleep(5.0)
   p.terminate() 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Run a function for X seconds

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap