How run a python script every x seconds - python

I have tried the schedule library however my code stalls sometimes which also means the schedule code stalls.
import schedule
import time
import app
schedule.every(3).seconds.do(app.run)
while 1:
try:
schedule.run_pending()
except Exception as e:
print e
pass
app.run is the script, it using Queues and Threads to request exchange data. One of the exchanges randomly throws an error and because the of threading (I think) the code goes into a state of limbo. I can't seem to fix the bug, but a dirty fix to the problem would be force run the script every x time (in my case I want 10 seconds) Any ideas ?

Besides the obvious way (have a thread that with while True: containing action() and sleep(10)), you can use threading.Timer or the sched module.

Related

2nd Function doesnt work until 1st function is completely executed in python

So basically, i have made a tkinter app that has a reminder utiliy in specific to generate notifications at the scheduled time. Everything works fine until I run the app module and another module having the notification generating function one at a time , but when I call the notification generating function intto the app module, my app doesnt work but the notification works. I want the app to run such that the notification generating function kind of runs in the background until the app module is open.
github link: https://github.com/click-boom/Trella
Looking into chatgpt i found terms like threading and multiprocessing, but i have no concept of that and still tried but didnt work.
Sure enough what you are looking for is multithreading.
Here is a simple example of how multithreading works (sorry for my lack of drawing skills).
This is how all monothread programs work. In most programming languages this is the default behaviour.
So in this example Second Task will have to wait for First Task to complete.
If you want several tasks to run concurrently, you can use multithreading.
This is how you could implement this in Python.
Monothreading:
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
firstTask()
secondTask()
Here, secondTask will only run after firstTask is done (i.e after 10 seconds).
Multithreading:
from threading import Thread
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
first_thread = Thread(target=firstTask)
second_thread = Thread(target=secondTask)
first_thread.start()
second_thread.start()
I hope this will be a help to someone !

Best way to constantly check for scheduled events on a website

So I am making a website, and something that required for part of the security is having a waiting period when trying to do something, for example trying to delete something, this would help incase someone's account was stolen and someone tried to ruin their account.
I'm already using SQLite so I'm going to create a table in there where scheduled events will be defined.
What I'm wondering is what is the best way to constantly check these scheduled events, it may also be important to note I want to check at least every hour. My immediate thought was creating a separate thread and running a function on there with a while loop in it which will constantly run a chunk of code with a time.sleep(3600) at the end of the function, like this:
def check_events(self):
while True:
# code
time.sleep(3600)
I'm not sure though if this is the most efficient way of doing it.
That function currently is inside my website code class hence the self, is that something I need to put on the outside or no?
I would either create a cron job on your server (which is the most straightforward)
or use a schedule module to schedule your task, see example:
import time
import schedule
from sharepoint_cleaner import main as cleaner
from sharepoint_uploader import main as uploader
from transfer_statistics import main as transfer_stats
schedule.every(1).hours.do(uploader)
schedule.every(1).hours.do(transfer_stats)
schedule.every().sunday.do(cleaner)
while True:
schedule.run_pending()
time.sleep(10)
https://github.com/ansys/automatic-installer/blob/4d59573f8623c838aadfd49c312eeaca964c6601/sharepoint/scheduler.py#L3

how to run python function again and again and never stop unless I stop it?

I am currently writing a python script. As I want it to check an API data in real time, so I want it to call the API function every 1 second. How should I done please?
You can use the following code
import time
while True:
call_api()
time.sleep(1)
here call_api will be called until you stop the program by ctrl+c

Python requests sometimes freezes

I have a Python program which sends several (about 5-6) long poll requests in parallel using different threads for each poll via requests package. And I realized that some of my threads sometimes just freeze. When this happens, the server I am sending the request does not receive the request. Also I set a timeout on the request and it does not work.
try:
print("This line prints")
response = requests.head(poll_request_url, timeout=180)
print("This line does not print when freeze occurs")
except ReadTimeout:
print("Request exception.")
except RequestException as e:
print("Request exception.")
except Exception:
print("Unknown exception.")
print("This line does not print either when freeze occurs.")
I am doing this on Raspberry Pi 2 hardware with Raspbian OS.
I used this same program without a problem when I was using Python 2.7. Recently I switched to Python 3.5. I tested using both requests versions with 2.8.1 and 2.9.1.
This problem does not occur very frequently but happens 2-3 times per day on different threads.
What might be the problem? How can I debug this?
Edit: The problem is solved by updating the Linux kernel.
According to the docs:
http://docs.python-requests.org/en/master/user/quickstart/#timeouts
It should be throwing a Timeout exception, when the timeout happens. That would mean that the line:
print("This line does not print when freeze occurs")
Would never be called it a timeout actually happens.
Are you catching the exception? Or any other exception? It might be that it's timing out fine, but you just aren't seeing this. Maybe try something like this:
try:
response = requests.head(poll_request_url, timeout=180)
except requests.exceptions.Timeout:
print("Timeout occurred")
So you can see if that's what is going on.
EDIT: possibly it's the "connect" step that's not timing out correctly. It may be the large timeout value for the "connect" step is messing it up somehow. Perhaps trying having a shorter timeout for that (as mentioned here):
http://docs.python-requests.org/en/master/user/advanced/#timeouts
e.g.
response = requests.head(poll_request_url, timeout=(3, 180))
Failing that it might be some sort of DNS lookup issue? Maybe see if hardcoding the IPs presents the same problem?
Solved my problem using timers (from threading import Timer). If no result next 10 seconds - repeat, if no result next 10 seconds - print 'Error' and go on. You can't monitor timer status with if statement if request freezes, but you can do it through while loop, adding time if result is ok (Python: Run code every n seconds and restart timer on condition).

Preventing hang with a timeout?

I've made a bot that uses the Selenium Webdriver Module to navigate a website. Unfortunately I've noticed that the script will sometime stop when it tries to click a button. The code is simple. I try to click the button, if I can't we wait a second and try again. It works, but at seemingly random times (sometimes after 10 minutes, sometimes after several hours) it just stops after clicking the button.
while 1:
try:
#Try to click the button
confirmButton = driver.find_element_by_name("confirm")
confirmButton.click()
#If we can't, wait a second and try again
except:
time.sleep(1)
I've been thinking about creating some way to detect this, thus being able to timeout the current attempt to click, but I can't seem to figure out how. The script of single threaded and I can't use the simple datetime technique, as it would never run that check, since it's still waiting for the button to have finished being clicked on.
Edit: Someone asked me how I knew it was hanging and not just retrying indefinitely. I did a test where I printed a number for each line that was executed and when I the hang occurred it would not execute any of he lines below confirmButton.click(). I think that is proof that it's hanging and not retrying indefinitely. Or maybe not?
Your problem can be solved with a timeout: launch a function in a separate thread and stops after a certain time limit if the function is not finished.
Here is the example
from threading import Thread
from time import sleep
def threaded_function():
confirmButton = driver.find_element_by_name("confirm")
confirmButton.click()
if __name__ == "__main__":
thread = Thread(target = threaded_function)
thread.start()
thread.join(1)# this means the thread stops after 1 second, even if it is not finished yet
print ("thread finished...exiting")
Hope that helps, and tell me if it does not solve the prob.

Categories