-2

I created a list which I filled only with zeros, sometimes I am writting ones in some cases of the list but my goal is to put the list to zero every 30 seconds and I don't know how to do it (I am a newbie in Python).

I found some parts of code on Internet and I assembled them. I am reading SIP prints, when I received "SIP/2.0 200 OK", it means terminal is active and I put 'one' in the case 25 if it's IP adress is 192.168.1.25. But the mobile can not be registered after being registered few times ago, so I would like to put the list to 0 every 30 seconds.

DjibTgy
  • 65
  • 1
  • 7
  • 1
    You want to set some kind of interval where the list resets to all 0s every 30s? Also, `List` is currently a dictionary, with a `len` of 0, so your for loop isn't doing anything. Maybe it would be a good idea to tell us what your wider intentions for the project are, and where you're stuck, instead of your idea of how to implement them, as you may need some wider advice on implementation in Python. – Izaak van Dongen Aug 23 '17 at 14:30
  • What do you mean "put to 0 the list after 30 seconds"? There is no while loop in your code so the program will terminate after the last line of code, in this case `List[number] =1`. Only 1 element in the list will be changed to a "1" from a "0" and the program will end. Additionally, `List` has no length so the for loop is not running. On another note, `List` is actually a dictionary so it should not be named `List`. – Advait Aug 23 '17 at 14:31
  • Can you explain, what you want to do? And why? – Dschoni Aug 23 '17 at 14:50
  • I edited my post to be clearer concerning what I want to do – DjibTgy Aug 23 '17 at 15:11
  • I still don't understand what the intent of the program is. Do you want to check for connection establishment? If so, use the socket library and try catch for connection. Over all, this seems to be an X-Y-Question: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Dschoni Aug 24 '17 at 14:28
  • So: Can you please explain the problem you have in the first place, not your attempted solution? Like: What problem do you want to solve with you code, posted above? – Dschoni Aug 24 '17 at 14:29
  • check this one https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python – Kallz Aug 28 '17 at 09:36
  • Does any of my answers solve your problem? – Dschoni Aug 29 '17 at 12:41

3 Answers3

0

To do something at a given interval of time, you can use timing functions.

import time
time.sleep(30)

will pause the execution of your code for 30 seconds. Note that this is blocking, so you cannot do something during this time (at least not in the same thread, and with multithreading/multiprocessing things ge a lot more difficult). So to put a zero to a list (in case this is, what you want to do) all 30 seconds do:

import time

list_of_values = []
while True:
    time.sleep(30)
    list_of_values.append(0)
Dschoni
  • 3,714
  • 6
  • 45
  • 80
  • I don't want to stop the execution of my program by using the function sleep(), I would like to initialize it to 0 each 30 seconds and by letting the program executing itself – DjibTgy Aug 24 '17 at 09:43
0

To do something every thirty seconds while doing something else, you can start a timer and check if 30 seconds have passed:

import time
start = time.clock()
while true:
  stop = time.clock()
  time_passed = stop-start
  if time_passed > 30:
    reset_list_to_zero_code_here
    start = time.clock() #Resets the timer.
  else:
    do_whatever_you_want_to_do
    time.sleep(0.1) #probably change this 

This sets a timer and each roundtrip checks, if 30 seconds have passed. If yes it runs whatever init script you put there and resets the timer. If not, it runs the line do_whatever_you_want_to. Depending on how fast this line executes, you may want to put an additional sleep behind it to time its execution (and probably save CPU load). However, this is a pretty basic approach and might be improved.

For understanding of timings, see also: Python - time.clock() vs. time.time() - accuracy?

Dschoni
  • 3,714
  • 6
  • 45
  • 80
0

You can create handler for it. just send alarm signal every 30 seconds and it will update list for you. below is sample example.

import signal
import time
class set_list:
    def __init__(self):
        signal.signal(signal.SIGALRM, self._set_list)
        signal.alarm(30)
    def _set_list(self,signum, frame):
        global list1
        print list1
        list1 = []
        signal.alarm(30)

set_list()
list1 = []

for i in range(10):
    list1.append(i)
    print list1
    time.sleep(6)

update your list in _set_list method according to your requirement. Hope this helps.

surya singh
  • 450
  • 2
  • 12