说检查点,其实就是对过去历史的记录,可以认为是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
执行完操作之后再写入到检查点文件中。以后程序挂掉,只要检查点文件还在,就可以延续前面的工作。不过这里的检查点相对于数据库中事务处理的检查点还是太简单了点。

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
