Python3.6 Schedule module scheduled task (instance description), python3.6schedule

Source: Internet
Author: User

Python3.6 Schedule module scheduled task (instance description), python3.6schedule

I. Programming Environment

PyCharm2016, Anaconda3 Python3.6

The schedule module needs to be installed at:

Open Anaconda Prompt and enter: conda install schedule. The message "Package Not Found Error" is displayed.

Therefore, pip is used for installation. Because Anaconda3 already has pip, for example:

Then, the cmd command line is switched to the scripts directory and pip.exe install schedule is successfully installed. In this way, you can import schedule in PyCharm.

2. Search for data from the database and write the data into the csv file at a specified time point every day.

① Use the sqlalchemy module to establish a database connection. for details about how to install the python3.6 for mysql driver in windows, refer to: Using Pycharm2016 to install the Mysql Driver Based on Anaconda3 Python3.6 in windows 10.

② Use the schedule module to execute periodic tasks. For the usage of this module, refer to the examples on the official website.

③ Use the csv module to write the queried records to a file

The complete code is as follows:

import scheduleimport codecsimport csvimport timefrom sqlalchemy import create_enginedef get_conn(): engine = create_engine("mysql+pymysql://root:password@localhost:3306/test?charset=utf8mb4") conn = engine.connect() return conndef query(): sql = "select * from user limit 10" conn = get_conn() return conn.execute(sql)def read_mysql_to_csv(filename): with, mode='w') as f:  write = csv.writer(f, dialect='excel')  results = query()  for result in results:   write.writerow(result)schedule.every()"17:49").do(read_mysql_to_csv, "test")while True: schedule.run_pending() time.sleep(10)

Iii. Summary

The schedule module is easy to implement: periodically execute tasks at a certain time point every day. The official example is as follows:

import scheduleimport timedef job(): print("I'm working...")schedule.every(10)"10:30").do(job)schedule.every()"13:15").do(job)while True: schedule.run_pending() time.sleep(1)

The above Python3.6 Schedule module scheduled task (instance description) is all the content shared by Alibaba Cloud xiaobian. I hope you can provide a reference and support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.