时间:2021-05-22
这篇文章主要介绍了Python3并发写文件原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用python2在进行并发写的时候,发现文件会乱掉,就是某一行中间会插入其他行的内容。
但是在使用python3进行并发写的时候,无论是多进程,还是多线程,都没有出现这个问题,难道是python3的特性吗?
import timeimport osimport multiprocessingfrom multiprocessing.dummy import Pool as ThreadPooldef write(val, file): w = open(file, "a") for i in range(100): w.write("%s\n" % val) time.sleep(0.001)def thread_write(file): res, pools = [], ThreadPool(10) for i in range(10): val = str(i) * 1000 res.append(pools.apply_async(func=write, args=(val, file, ))) while res: for ret in res: if ret.ready(): res.remove(ret) time.sleep(0.01)def mutil_write(file): pools = multiprocessing.Pool(processes=10) res = [] for i in range(100): res.append(pools.apply_async(thread_write, args=(file, ))) while res: for ret in res: if ret.ready(): res.remove(ret) time.sleep(0.01)if __name__ == '__main__': file = "./write_test" mutil_write(file) with open(file) as fb: lines = 0 line_len = [] for line in fb: lines += 1 line = line.strip() line_len.append(len(line)) if len(line) != 1000: raise(Exception("error line: %s, len: %d" % (line, len(line)))) print("lines:%d, max len:%d, min:%d, avg:%.2f" % (lines, max(line_len), min(line_len), sum(line_len)/len(line_len))) os.remove(file)上面代码,多进程并发写结束后,校验每一行的长度是否是设置好的长度。用python3反复运行,均通过测试没有异常。
$ python3 --versionPython 3.7.4$ python3 t.pylines:10000, max len:1000, min:1000, avg:1000.00如果用python2,则会出现异常:
$ python2 --versionPython 2.7.15$ python2 t.pyTraceback (most recent call last): File "t.py", line 49, in <module> raise(Exception("error line: %s, len: %d" % (line, len(line))))Exception: error line: 333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, len: 1092以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
python3的编码问题。打开python开发工具IDLE,新建‘codetest.py'文件,并写代码如下:importsysprint(sys.getdef
最近写了一些python3程序,四处能看到bytes类型,而它并不存在于python2中,这也是python3和python2显著区别之一。以前在写python
想学习Python的人都会有一个困惑,那就是Python目前有两个版本Python2和Python3,Python2与Python3有何区别,两个版本该学习哪个
最新的CentOS8已经内置了Python2和Python3,出入Python2和Python3两个命令可以分别进入Python2和Python3。CentOS
现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2),所以我用Python3的语法写了一个