如何在x分钟后暂停此线程

时间:2016-10-04 22:54:30

标签: python multithreading bots python-multithreading

如何暂停此线程每300秒运行500秒?我需要暂停它每隔x分钟运行所有函数以防止API违规。计时器对象似乎只启动代码,而目标是暂停,等待,恢复。

from TwitterFollowBot import TwitterBot
from threading import Thread
import random

my_bot = TwitterBot()
my_bot.sync_follows()


def a():
    my_bot.auto_fav("@asco", count=1000)

def b():
    my_bot.auto_fav("ATSO", count=1000)

def c():
    my_bot.auto_fav("BCY3", count=1000)

lof = [a, b, c]
random.shuffle(lof)

for z in lof:
    Thread(target=z).start()

感谢@ Farhan.K协助代码。

1 个答案:

答案 0 :(得分:1)

你的主题可能是这样的吗?

import time


def sleep_thread(sleepWait, sleepTime):
    timeStart = time.time()
    timeElapsed = 0 

    while timeElapsed <= sleepWait:
        timeElapsed = time.time() - timeStart
        print 'time elapsed = ' + str(timeElapsed)
        time.sleep(1)

    print 'going to sleep. zzz....'
    # Sleep for x 
    time.sleep(sleepTime)
    print 'im awake!'


sleep_thread(5, 3)