So I want to import * from a phyton file. However, these variables I want to import are connected to an API. So they change.
For instance:
at 3:35 is A=5
at 3:36 is A=6
So I want my import to be done every 15 second. How do I write this?
Just use it this way where I just show the schedule to show how the variables can be changed in two files and the schedule represented the change of the API.
In the file1.py
import schedule
import time
def job(t):
var1=5
print(var1)
schedule.every().day.at("3:35").do(job,'It is 3:35')
while True:
schedule.run_pending()
time.sleep(15) # wait one minute
In the file2.py
from file1 import var1
import schedule
import time
def job(t):
var1=6
print(var1)
schedule.every().day.at("3:36").do(job,'It is 3:36')
while True:
schedule.run_pending()
time.sleep(15) # wait one minute
Related
I wrote this script a few days ago in VSCode. It's supposed to open my zoom meetings every day at a certain time and I really want it to work because it's one of my first projects. I think I wrote everything correctly, however. it won't work. Do you see what I'm missing?
import webbrowser
import schedule
import time
url_deutsch = "https://us04web.zoom.us/j/79379252675?pwd=cm1NUUlSMlg4aSsyWXVwL3UybzhOUT09"
url_chemie = 'https://zoom.us/j/95643744370?pwd=clRkMDNEVTZ3dzErZWUvZWZFVFVvQT09'
url_englisch = 'https://us02web.zoom.us/j/3470923893?pwd=ejdIaVdaL21yWXVPaU9wdnBDalhYUT09'
url_opv = 'https://us04web.zoom.us/j/79683686777?pwd=QXhmVTZFZitCbUowZTh1bDFTWjRVQT09'
url_test = 'google.com'
chrome_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe %s'
#Zoom aleman
def open_link_deutsch():
webbrowser.get(chrome_path).open(url_deutsch)
return
#Zoom quimica
def open_link_chemie():
webbrowser.get(chrome_path).open(url_chemie)
return
#Zoom ingles
def open_link_englisch():
webbrowser.get(chrome_path).open(url_englisch)
return
def open_link_test():
webbrowser.get(chrome_path).open(url_test)
return
while True:
schedule.run_pending()
time.sleep(0)
#Schedule
schedule.every().monday.at('13:30').do(open_link_deutsch)
schedule.every().tuesday.at('11:00').do(open_link_deutsch)
schedule.every().wednesday.at('09:10').do(open_link_deutsch)
schedule.every().wednesday.at('07:15').do(open_link_chemie)
schedule.every().tuesday.at('07:15').do(open_link_englisch)
schedule.every().friday.at('09:10').do(open_link_englisch)
schedule.every().friday.at('12:51').do(open_link_test)
After you run this code, it will not wait for the events that you sheduled.
According to official schedule documentation on PyPI, you should add
import time
to imports and something like
while True:
schedule.run_pending()
time.sleep(1)
at the end of your code. This will execute forever and check if any event is pending for this exact moment every second.
You need to set the schedule before entering the while-loop. The code is doing something, it's checking to see if any jobs are scheduled, constantly. Move the schedule.every() calls before the loop, and choose a more reasonable time to wait between schedule polls. The code below polls about every 60s which I think is fine, but you could make it poll more frequently.
import schedule
import time
import webbrowser
#Zoom quimica
def open_link_chemie():
webbrowser.get(chrome_path).open(url_chemie)
return
#Zoom ingles
def open_link_englisch():
webbrowser.get(chrome_path).open(url_englisch)
return
def open_link_test():
webbrowser.get(chrome_path).open(url_test)
return
#Schedule
schedule.every().monday.at('13:30').do(open_link_deutsch)
schedule.every().tuesday.at('11:00').do(open_link_deutsch)
schedule.every().wednesday.at('09:10').do(open_link_deutsch)
schedule.every().wednesday.at('07:15').do(open_link_chemie)
schedule.every().tuesday.at('07:15').do(open_link_englisch)
schedule.every().friday.at('09:10').do(open_link_englisch)
schedule.every().friday.at('12:51').do(open_link_test)
while True:
schedule.run_pending()
time.sleep(60)
I am trying to put a few python scripts to scheduled and run in main.py. Those scripts are put in the same folder.
main.py:
import schedule
import time
from test1 import dd
schedule.every(2).seconds.do(dd,fname)
while True:
schedule.run_pending()
time.sleep(1)
test1.py:
def dd(fname):
print('hello' + fname)
dd('Mary')
dd('John')
It run out as those 2 name and name 'fname' is not defined.
How to define the argument at main.py file? If I have more than one def in the script, shall I need to import multiple times in the main.py
and the script that I import at top of main.py, it run once before running the schedule? That mean it will run one while you import it?
You are not defining your fname in main.py so it says name 'fname' is not defined. You are only importing the functions to main.py from test1.py
Here is the modified code:
main.py
import schedule
import time
from test1 import dd
fname="Mary"
schedule.every(2).seconds.do(dd,fname)
while True:
schedule.run_pending()
time.sleep(1)
test1.py
def dd(fname):
print('hello' + fname)
if you want to input more than one string, just simply use a list! Here is the sample code for test1.py:
def dd(fname:list):
for n in fname:
print('hello' + n)
These codes are tested using Python 3.7.7
Your problem is that you are trying to use a function argument as it's own variable. Importing is not the problem here.
Try this:
import schedule
import time
from test1 import dd
schedule.every(2).seconds.do(dd,("Any String",))
while True:
schedule.run_pending()
time.sleep(1)
import schedule
import time
def job(work):
print(work)
schedule.every().day.at("10:30").do(job(work))
while True:
schedule.run_pending()
time.sleep(1)
How to call job(work) inside my do() function. if i give job() it works fine, but if i give job(work) it throwing error. what to do with this? any help
This is in the schedule FAQ:
import schedule
import time
def job(work):
print(work)
schedule.every().day.at("10:30").do(job,work="") # make "" whatever string you want
while True:
schedule.run_pending()
time.sleep(1)
I want to have a function run every 10 seconds. The script is scheduled on Airflow to run between 9 am to 11 am.
If the condition is met, the process schedule will stop. However, it seems the function only ran once. Can anyone help let me know what I should modify to make it work?
'''
import pandas as pd
import sched, time
import datetime
import smtplib
import io
'''
'''
def timedTask():
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(10, 1, task)
scheduler.run()
'''
'''
def task():
dq_value = % sql
if dq_value > 1:
return
else:
print("wait")
'''
'''
timedTask()
'''
I'm trying to use the APScheduler library to execute a job at two certain times: 1am PST and 2am PST.
Sort of lost, I am trying to figure out what am doing wrong. Here is my code so far:
#!/usr/bin/python
import sys
import re
import urllib
import csv
import urllib2
import logging
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
logging.basicConfig()
def main():
#Make a rest call, store the output in a csv, close the file.
...
f.close()
sched.add_job(my_job, 'date', run_date = '2015-12-15 01:00:00', args=['text'])
try:
sched.start()
print(datetime.datetime.now())
except (KeyboardInterrupt):
logger.debug('Terminating...')
print (datetime.datetime.now())
The current time is 12:16 PST, I just ran the above script and it executed and created the file prior to the job time of 12:30:30pm; this is for testing purposes.
So, I'm trying to figure out what I am doing wrong with regards to using APScheduler.
I want to improve this code, what are some things I can change?
If you want to run your code at certain times every time, you should be using the cron trigger:
sched.add_job(my_job, 'cron', hour='1,2', args=['text'])
For better debugging:
logging.basicConfig(level=logging.DEBUG)