简体   繁体   中英

Python script isn't scheduling

I have a script which responds to tweets sent to a handle. The problem is it only runs once, upon initiation in the Terminal, and ceases to work. I've scheduled it to run every minute using the Python scheduler but it isn't working.

import sched, time, tweepy
#from our keys module (keys.py), import the keys dictionary
from keys import keys

s = sched.scheduler(time.time, time.sleep)

def run_thejewels(sc):

    CONSUMER_KEY = keys['consumer_key']
    CONSUMER_SECRET = keys['consumer_secret']
    ACCESS_TOKEN = keys['access_token']
    ACCESS_TOKEN_SECRET = keys['access_token_secret']

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)

    twt = api.search(q='@twitterhandle')

    #list of specific strings we want to check for in Tweets
    t = ['TLOP',
         'FS4',
         'ULB']

    u = ['HL',
         'FSMH',
         'FADE']

    for s in twt:
        for i in t:
            if i in s.text:
                sn = s.user.screen_name
                m = "@%s Hello bruv" % (sn)
                s = api.update_status(m, s.id)

    for p in twt:
        for j in u:
            if j in p.text:
                pn = p.user.screen_name
                m = "@%s Hello bro" % (pn)
                p = api.update_status(m, p.id)

s.enter(60, 1, run_thejewels, (s,))
s.run()

The scheduler's enter method sets up a single , one-time event in the scheduler, not a repeating event. So you haven't "scheduled it to run every minute", even if you intended to.

I don't think sched has any facility for scheduling repeating events. So what you will need to do is to make the function it calls -- in your case, run_thejewels -- reschedule another instance of the event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM