2

i'm trying to schedule a task every 5 seconds, here what i did:

import schedule
import time
import tweepy 
from threading import Timer

def job():
  iGen = (i for i in range(1, 6))
  for i in iGen:

    i += 1
    mymessage = "My message here " + str(i)

    print(mymessage)

schedule.every(5).seconds.do(job)

while 1:                                
   schedule.run_pending()
   time.sleep(1)

but the result is:

My message here 2 ..after 5 secs
My message here 3
My message here 4
My message here 5
My message here 6
My message here 2 ..after 5 secs
My message here 3
My message here 4
My message here 5
My message here 6
My message here 2 ..after 5 secs
My message here 3
My message here 4
My message here 5
My message here 6

what i need is:

My message here 2 ..after 5 secs
My message here 3 ..after 5 secs 
My message here 4 ..after 5 secs
My message here 5 ..after 5 secs
My message here 6 ..after 5 secs

sorry for the newbie question, Thank you

2
  • What do you need this for? There might be a better way to accomplish this. Commented Sep 5, 2015 at 20:19
  • auto message to be sent with an auto incremented number, please, i'm open to suggesting Commented Sep 5, 2015 at 20:23

3 Answers 3

2

Your job is to loop over 2-6, printing for each. It sounds like you want the job to just print once each time it runs. This would do that, but would not number the messages.

import schedule
import time

def job():
    print("Message")

schedule.every(5).seconds.do(job)

while 1:                                
   schedule.run_pending()
   time.sleep(1)

To get numbering is a bit more complicated, but you can do it with a static variable:

import schedule
import time

def job():
    job.i += 1
    print("Message: " + str(job.i))
job.i = 1

schedule.every(5).seconds.do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
Sign up to request clarification or add additional context in comments.

2 Comments

after testing your code the result is every 5 secs : "Message: 2"
@MounirElfassi You are absolutely right. I was using generators totally wrong. I have edited my answer, and now works.
2

If you just need to increment a number and print it every X seconds you can try something like this:

i = 0
while 1:
    print("My message here "+str(i))
    i++
    time.sleep(5)

Does it have to be threaded?

I will edit this answer if you give me more detail on how your application should work.

3 Comments

but how can i run this every x secs?
sorry i get it now, this was simple.. Thanks
Yeah it's the while true and the time.sleep which make it run every 5 seconds.
2

I think your problem is with you the job function!Ask yourself whether looping through 2 to 5 is a part of every job method or not?According to your problem description,it should only contains printing a message:

def job(msg,num):  #if your message is same you won't need to pass it,same for num
    print(msg+str(num)) 

then in other parts you should pass message and num :

schedule.every(5).seconds.do(job)

while 1:                                
   schedule.run_pending()
   time.sleep(1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.