python设置检查点简单实现代码
说检查点,其实就是对过去历史的记录,可以认为是log.不过这里进行了简化.举例来说,我现在又一段文本.文本里放有一堆堆的链接地址.我现在的任务是下载那些地址中的内容.另外因为网络的问题或者网站的问题,每次下载可能不会非常的成功.有可能出现断链或者socket异常错误。不过不管产生什么样的错误,我都希望我的程序能够一直跑下去。或者能停掉后,继续从为下载的链接处跑。而不是从开始的地方跑。这个问题非常简单。因为这些链接是上下文无关的(上下文有关的情况要另外分析)。所以我只要记录程序运行停止前的最后一条,就有希望能够延续前面的工作。这里实现中使用的是记录原有的链接,大家也可以使用计数器的方法来记录。代码如下:
# 这个异常是原文本内容中未出现检查点内容出现造成的 class CheckPointMissContentError: pass # 将文件读取指针fd移至到检查点对应的内容处 # check point 的规则为,读取文件一行或者多行,进行操作后,将此一行或多行送入 # 检查文件check_point中。以后再次运行程序,即可从该检查点处继续运行。 def GoCheckPoint(fd,check_point): if not os.path.isfile(check_point): f_check = open(check_point,'w') f_check.close() f_check = open(check_point,'r') lines = f_check.readlines() if len(lines) > 0: check_content = lines[-1] #找到检查点最后一行 check_content = check_content.strip(' /n/r') # go to check point while True: content = fd.readline() if content == '': # eof raise CheckPointMissContentError if content.strip(' /n/r') == check_content: break f_check.close()#关闭检查点
有了上面一段还是不够的,需要下面的代码补充:
# 伪代码 def Download(downloadlist,sleep_time): if os.path.isfile(downloadlist): f = open(downloadlist) # check_point file name,这里为自动生成一个检查点文件 check_point = file[0:file.rfind('.')]+'_check.txt' Util.GoCheckPoint(f,check_point) #这就是上面代码中的GoCheckPoint函数 f_check = open(check_point,'a')# 以追加方式写入 try: while True: content = f.readline() if content == '': # eof break content = content.strip(' /n/r') if content != '': # has download url time.sleep(sleep_time) DownloadOper(path,url) #这里是伪代码..可以认为是urllib.request.retrieve()函数或者是urllib.request.urlopen()啥的 # 作为响应的操作后再将内容写入检查点文件 f_check.write(content+'/n') f_check.flush() # 必须的,否则会缓存,不会写入硬盘中 except : # 蹦个异常也不怕,以后再次按F5执行即可 raise Exception() return Util.FAILURE # 这是我设置的常量,大家认为是0或者1就可以了 finally: f.close() f_check.close()# 关闭文件 print('Downloading is done........................') return Util.SUCCESS
执行完操作之后再写入到检查点文件中。以后程序挂掉,只要检查点文件还在,就可以延续前面的工作。不过这里的检查点相对于数据库中事务处理的检查点还是太简单了点。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

MySQL Workbench can connect to MariaDB, provided that the configuration is correct. First select "MariaDB" as the connector type. In the connection configuration, set HOST, PORT, USER, PASSWORD, and DATABASE correctly. When testing the connection, check that the MariaDB service is started, whether the username and password are correct, whether the port number is correct, whether the firewall allows connections, and whether the database exists. In advanced usage, use connection pooling technology to optimize performance. Common errors include insufficient permissions, network connection problems, etc. When debugging errors, carefully analyze error information and use debugging tools. Optimizing network configuration can improve performance

For production environments, a server is usually required to run MySQL, for reasons including performance, reliability, security, and scalability. Servers usually have more powerful hardware, redundant configurations and stricter security measures. For small, low-load applications, MySQL can be run on local machines, but resource consumption, security risks and maintenance costs need to be carefully considered. For greater reliability and security, MySQL should be deployed on cloud or other servers. Choosing the appropriate server configuration requires evaluation based on application load and data volume.
