Edit: Thank you very much guys. This code is finally working:
# imports
from pynput.keyboard import Key,Controller
import time
import threading
keyboard = Controller()
a=int(input("Enter the number of loops to send: "))
arr=["pls beg","pls fish","pls hunt","pls postmemes","c","pls search","hospital", "pls highlow", "high", "pls trivia", "C", "pls deposit all"]
print("Please take the cursor where you want to spam the message, spamming will start in 5 seconds...")
time.sleep(5)
num=0
def doFunc(function, timeToSleep):
for num in range(a):
# we pass functions because the class is not initialised
function(functions, timeToSleep)
# the function with classes
class functions:
# example functions
def function1(self, timeToSleep): #pls beg command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[0])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function2(self, timeToSleep): #pls fish command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[1])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function3(self, timeToSleep): #pls hunt command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[2])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function4(self, timeToSleep): #pls postmemes command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[3])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[4])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function5(self, timeToSleep): #pls search command (cooldown of 30s)
time.sleep(timeToSleep)
keyboard.type(arr[5])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[6])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function6(self, timeToSleep): #pls highlow command (cooldown of 20s)
time.sleep(timeToSleep)
keyboard.type(arr[7])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[8])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function7(self, timeToSleep): #pls trivia command (cooldown of 10s)
time.sleep(timeToSleep)
keyboard.type(arr[9])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
keyboard.type(arr[10])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
def function8(self, timeToSleep): #pls deposit all command (cooldown of 45s)
time.sleep(timeToSleep)
keyboard.type(arr[11])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
# change the times you want each function to sleep
timesDict = {
functions.function1: 32,
functions.function2: 31,
functions.function3: 33,
functions.function4: 34,
functions.function5: 35,
functions.function6: 21,
functions.function7: 11,
functions.function8: 45
}
# start threads
for f, t in timesDict.items(): threading.Thread(target=doFunc, args=(f, t)).start()
How can I make a python program for doing a few tasks repeatedly for same number of times (can use a simple for loop here) but each having independent time intervals? For example, I want function 1 to be executed every 30 seconds, function 2 to be executed every 17 seconds, function 3 to be executed every 36 seconds, and so on. I tried doing it using time.sleep()
, but I couldn't make it work since their time intervals are independent of each other.
Check this blogpost . I think you have to use threading and multiprocessing for your case.This basically creates child process to run your function independently.
OR You can try this one
def function(): #pls beg command (cooldown of 30s)
keyboard.type(arr[0])
keyboard.press(Key.enter)
keyboard.release(Key.enter)
return 1 #You return your function
# then in main function inside the loop use this
if function():
time.sleep('30 secs')
You can use threads
# imports
import time
import threading
def doFunc(function, timeToSleep):
while True:
# we pass functions because the class is not initialised
function(functions, timeToSleep)
# the function with classes
class functions:
# example functions
def function1(self, timeToSleep):
time.sleep(timeToSleep)
print('func 1 finished')
def function2(self, timeToSleep):
time.sleep(timeToSleep)
print('func 2 finished')
# change the times you want each function to sleep
timesDict = {
functions.function1: 5,
functions.function2: 1
}
# start threads
for f, t in timesDict.items(): threading.Thread(target=doFunc, args=(f, t)).start()
# output
>>> 'func 2 finished'
>>> 'func 2 finished'
>>> 'func 2 finished'
>>> 'func 2 finished'
>>> 'func 1 finished'
# if i understood the question correctly, this is what you want
Create for every function a variable like last_running last_running_2 ect...
And you check every time if time.time()-last_running >= 30
then 30 seconds have passed and you can run the function. it will look like this:
while True:
if time.time()-last_running >= 30:
last_running = time.time() #you update the last running every time
function()
if time.time()-last_running_2 >= 17:
last_running_2 = time.time()
function(2)
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.