文件读写,多线程、多进程

import time,os,threading,random

def file_read(path):
try:
with open(path, 'r') as f:
# str = f.read()
# print(str)
for line in f.readlines():
print(line.strip()) # while True:
# l = f.readline()
# if l == '':
# break
# print(l.strip())
except Exception as e:
print('Error: ', e) def file_write(path, str):
try:
with open(path, 'a') as f:
f.write(str)
except Exception as e:
print('Error: ', e)
# 文件读写
# file_write('d:/test.txt', time.strftime('%Y-%m-%d %H:%M:%S')+' test\n')
# file_read('d:/test.txt') # 多线程 多进程
import multiprocessing
lock = threading.Lock()
def run_thread1():
lock.acquire()
print('Process (%s) thread %s is running...' % (os.getpid(), threading.current_thread().name))
time.sleep(3)
lock.release() def run_thread2():
lock.acquire()
print('Process (%s) thread %s is running...' % (os.getpid(), threading.current_thread().name))
time.sleep(1)
lock.release() def long_time_task(name):
print('Run task %s (%s)...' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print('Task %s runs %0.2f seconds.' % (name, (end - start))) if __name__ == '__main__':
t1 = threading.Thread(target=run_thread1)
t2 = threading.Thread(target=run_thread2, name='Jincheng2')
t1.start()
t2.start()
t1.join()
t2.join() p1 = multiprocessing.Process(target=run_thread1)
p1.start()
p1.join() # 进程池创建子进程
p = multiprocessing.Pool(4)
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print('Waiting for all subprocesses done...')
p.close()
p.join()
print('All subprocesses done.')
# 子进程subprocess,进程间通信Queue、Pipes

  

Python练习笔记(2)的更多相关文章

  1. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  2. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  3. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  4. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  5. python datetime笔记

    python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...

  6. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  7. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  8. python学习笔记--Django入门0 安装dangjo

    经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...

  9. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  10. python自学笔记

    python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...

随机推荐

  1. matlab中repmat函数的用法(堆叠矩阵)

    matlab中repmat函数的用法 B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p...]) 这是一个处理大矩阵且内容有重复时使用 ...

  2. 互联网部-供应商团队js规范

    文件命名 页面.js文件命名:采用中划线连接方式,比如说: error-report.html js文件夹命名:scripts 公共js命名:common.js 文件结构组织 每个页面中中独有的js文 ...

  3. File not Found:DockForm.dcu的解决办法

    安装控件时,如果引用了dsgnintf单元,那么就会提示找不到proxy.pas 或者DockForm.dcu的错误,只需在安装控件包时添加“lib\DesignIde.dcp”即可

  4. vue+node+mongodb实现的功能

    用vue+node +mongodb实现前后台交互的页面代码,已经上传到github上, 地址是: https://github.com/GainLoss/vue-node-mongodb https ...

  5. MHA 一主一从搭建

    安装基本环境介绍 一主一从     MHA manager & slave  node2 192.168.56.27 Master机器 node1 192.168.56.26 VIP   19 ...

  6. ART_20190430

    Algorithm-算法题 第一个只出现一次的字符 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要 ...

  7. css实现单行的靠左靠右和居中效果

    1.父元素    text-align:center 2.子元素 .left{ float:left; } .right{ float:right; } .center{ display:inline ...

  8. genil层

    genil 层将底层的业务逻辑封装成一个接口(例如 get_dynamic_result这种),供ui层调用(ui点击 search dynamic result按钮,会调用 get_dynamic_ ...

  9. 七、IntelliJ IDEA 常见文件类型的图标介绍

    咱们已经了解了很多关于 IntelliJ IDEA 的内容啦,例如,在 Windows 系统下安装 IntelliJ IDEA.运行 IntelliJ IDEA .创建 Java 项目以及修改 Int ...

  10. java 枚举类型enum

    简单介绍 Java 中的枚举类型采用关键字enum 来定义,从jdk1.5才有的新类型,所有的枚举类型都是继承自Enum 类型.要了解枚举类型,建议大家先打开jdk 中的Enum 类简单读一下,这个类 ...