-3

As you see in the below code, it is possible to open a file in a directory and read it. now i want live_token read the file every 30 minutes and print it. Can anyone help me in this regard? I found below code as scheduling to do a job but i don't know how to do needful modifications.

schedule.every(30).minutes.do()

Sorry if this question is so basic, I am so new with Python.

def read_key():
    live_key_file_loc = r'C:\key.txt'
    live_key_file = open(live_key_file_loc , 'r')
    global key_token
    time.sleep(6)
    live_token=live_key_file.read()
    print(live_token)
Saher
  • 57
  • 1
  • 6
  • Do you mean you want to run the read_key function every half hour? Then `.do(read_key)`. – jonrsharpe Jul 27 '20 at 06:28
  • no i want live_token read the file every 30 minutes – Saher Jul 27 '20 at 06:29
  • Get help from this link: https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds. You just need to convert this in minutes, and you can do your stuff as is. – Alok Jul 27 '20 at 06:31
  • And that's what read_key does, so it's not clear to me what distinction you're drawing. – jonrsharpe Jul 27 '20 at 06:31
  • @jonrsharpe: as you see first i introduce file location, then i define a variable to open it, now live_token is going to read live_key_file . right? i want live_token do this reading function every 30 minutes. – Saher Jul 27 '20 at 06:34
  • why not use cron job? – Underoos Jul 27 '20 at 06:57

3 Answers3

1
import time

sleep_time = 30 * 60  # Converting 30 minutes to seconds


def read_key():
    live_key_file_loc = r'C:\key.txt'
    live_key_file = open(live_key_file_loc, 'r')
    global key_token
    time.sleep(6)
    live_token = live_key_file.read()
    print(live_token)

while(True):  # This loop runs forever! Feel free to add some conditions if you want!

# If you want to read first then wait for 30 minutes then use this-
read_key()
time.sleep(sleep_time)

# If you want to wait first then read use this-
time.sleep(sleep_time)
read_key()
Dharman
  • 30,962
  • 25
  • 85
  • 135
CODER_SCREAM
  • 63
  • 1
  • 1
  • 8
  • As i see in this code the method is running each 30 minutes and read from key location file. is there any way to just update the value of live_token not the method. I mean as you see use live_token = live_key_file.read(). is there any way to refresh the value of live_token every 30 minutes? – Saher Jul 27 '20 at 07:13
  • What do you mean by just updating the value? You have to call the function and then read the value again, else how will you update the value? Sorry for late reply. – CODER_SCREAM Aug 04 '20 at 16:57
0

@jonrsharpe is right. Refer to schedule usage. You should have a script which should keep running always to fetch the token every 30 minutes from the file. I have put below a script which should work for you. If you dont want to run this file in python always, look for implementing a scheduled job.

import schedule
import time

def read_key():
    with open('C:\\key.txt' , 'r') as live_key_file_loc
        live_token = live_key_file_loc.read()
    print(live_token)

schedule.every(30).minutes.do(read_key)

while True:
    schedule.run_pending()
    time.sleep(1)
Abhijeetk431
  • 847
  • 1
  • 8
  • 18
  • @jonrsharpe is right. Also you need to use the schedule as per its proper usage. – Abhijeetk431 Jul 27 '20 at 06:36
  • Thanks for this code. is it possible to let live_token to be read every 30 miutes. I want to update live_token value. – Saher Jul 27 '20 at 06:39
  • This will read the live_token every 30 minutes and print it. It will not keep the file open for the entirety of 30 minutes, so a separate program can modify the token and the same will be reflected in this program on the next read. – Abhijeetk431 Jul 27 '20 at 06:41
  • the only problem is, I just want to update the value of live_token. not the method. cause i am calling live_token in other lines of code the method is being updated but the value of live_token is not! I want to have modification of 30 minutes updating in this line: live_token = live_key_file_loc.read() . – Saher Jul 27 '20 at 07:23
  • If you want to update the token every 30 minutes, create a new method write_key, which instead of reading the key from the file, writes the key to the file, and schedule that write_key method to execute every 30 minutes. Meanwhile other part of your code can continue using the read_key method to fetch the token from the file.. Does this make sense or am I missing something here?? – Abhijeetk431 Jul 27 '20 at 07:29
  • the point is the token is seperately goes to key.txt every 30 minutes by different code and it is scheduled by other team. now i just want to read it every 30 minutes. I just didnt get why i should write it again in another text file. sorry if i disturb. – Saher Jul 27 '20 at 07:34
  • No in that case you should not.. Sorry I got confused by your previous comment. If some other code is responsible for writing token to file then this answer will work for you to get the token from that file. The method will not get updated, it will keep executing every 30 minutes and keep fetching the updated token everytime. – Abhijeetk431 Jul 27 '20 at 07:39
  • I run this code and it works fine, Just is scheduling has any mismatches with flask, cause when i put it in flask and run python it seems only once this code has been read! – Saher Jul 27 '20 at 09:29
  • I think that might be because your flask app is running in the main thread and this method is not... It seems what might suite your use case is to have a separate thread that checks and exports the token and then sleeps for 30 minutes. Check multithreading module. – Abhijeetk431 Jul 27 '20 at 10:00
0

There are a few steps in this process.

  1. Search for “Task Scheduler” and open Windows Task Scheduler GUI.

  2. Go to Actions > Create Task…

  3. Name your action.

  4. Under the Actions tab, click New

  5. Find your Python Path by entering where python in the command line. Copy the result and put it in the Program/Script input.

  6. In the "Add arguments (optional)" box, put the name of your script. Ex. - in "C:\user\your_python_project_path\yourFile.py", put "yourFile.py".

  7. In the "Start in (optional)" box, put the path to your script. Ex. - in "C:\user\your_python_project_path\yourFile.py", put "C:\user\your_python_project_path".

  8. Click “OK”.

  9. Go to “Triggers” > New and choose the repetition that you want. For more details check this site -

    https://www.jcchouinard.com/python-automation-using-task-scheduler/

Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25