Python Learning Timing Modification Database detailed

Source: Internet
Author: User

in thePythonThe development of database modification is often the case, today and we share is the time to modify the database of those things, I hope that the firstGetting StartedPython's children's shoes are helpful.when we need to modify the database regularly, we usually choose a timed process to change the library. If this timed task is written to the business and written as an interface, the timing process seems a bit inappropriate? If you need to modify it regularly100Secondary database, general practice will start -process, although this process is very lightweight, it still feels uncomfortable. We can actually useThreading. Timerthe idea of creating the appropriate thread to perform the library operation is also relatively straightforward. 1.incoming time to perform a change to the library operationUpdate_time, withUpdate_timeminus the current time, how much time does it have to get away from the library operation?Time_delay. Two standard time format strings can be used for the difference betweendatetime.datetime.strptime ()to format the time, the time after the format can be directly subtracted, the results are executed. Seconds ()it can be converted into seconds . 2.encapsulating the library operation as a methodUpdate (), and then theUpdateand the time difference passed inThreading. TimerCreate a thread that uses theThreading. Timer (interval, function, args=[], kwargs={})Create a thread instance,intervalis the time to delay execution, in seconds, and then, start ()execution. Timeris non-blocking and can create multiple threads that do not affect each other. The code is as follows#!/usr/bin/env python3#-*-coding:utf-8-*- fromModelImportTable fromHandler.base_handlerImportBasehandler fromThreadingImportTimerImportDatetime classTimehandler(Basehandler): defdo_action(self): Update_time = "2018-04-07 18:00:00" ads_id = "Test_1" T_online = Datetime.datetime.strptime (Update_time, '%y-%m-%d%h:%m:%s ') now = Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') T_now = Datetime.datetime.strptime (now, '%y-%m-%d%h:%m:%s ') Time_delay = (t_online-t_now). seconds T1 = Timer (Time_delay, Self.update, (ads_id,)) T1.start () Self.result = "Success" return defUpdate(Self, ads_id): Self.db.dsp.query (Table). Filter (table.ads_id = = ads_id). Update ({table.is_del:0}) Self.db.dsp.commit ()can addUpdate_timeTo the front-end incoming parameters, you can perform the change operation at that time. At that time encountered a small pit, that is, the library operation did not take effect, because the last line is not addedcommit (). Originally changed the libraryCommitthe entry into force is written in the base classBasehandlerHeavy, but here's theUpdate ()in theTimerexecuted in a thread, which is an asynchronous operation that requires executioncommit ()make the changes effective. this withTimerThe method of timing execution is lighter and simpler than the traditional timing process, but it also has obvious drawbacks. When the service shuts down, all timed threads are destroyed along with the main process, and all threads are successfully executed with the prerequisite that the service must be stable and cannot be restarted. If you want to restart the service, you will need to find ways to drop the unfinished task(like writing to a database.), and then start the service when you read the unfinished task before you recreate the timed thread. Source: Network

Python Learning Timing Modification Database detailed

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: info-contact@alibabacloud.com 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.