python 线程,进程28原则
基于函数实现
from threading import Thread def fun(data, *args, **kwargs):
""" :param data:
:param args:
:param kwargs:
:return:
"""
print('start %s'%data) if __name__ == "__main__":
tups = ((1,),(2,),(3,))
for i in tups:
kwargs = {
"target": fun,
'args':i
}
t = Thread(**kwargs)
t.start()
基于类实现
from threading import Thread
from multiprocessing import Process class MyThread(Thread):
def __init__(self, func,*args):
super(MyThread,self).__init__()
self.func = func
self.args = args def run(self):
self.func(*self.args) class MyProcess(Process):
def __init__(self, func,*args):
super(MyProcess, self).__init__()
self.func = func
self.args = args def run(self):
self.func(*self.args)
传入类的入口函数,即可实现多线程
baidu = Baidu()
sogou = Sogou()
google = Google() baiduThread = MyThread(baidu.run,1,100)
sogouThread = MyThread(sogou.run,1,100)
googleThread = MyThread(google.run,1,100)
# 线程开启
baiduThread.start()
sogouThread.start()
googleThread.start()
# 主线程等待子线程
baiduThread.join()
sogouThread.join()
googleThread.join()
总结一波,实际实现多进程的进程是类的实例化对象的run函数(baidu.run),因此在初始化类时是串行的,而很多模块并不能在同一进程中实例化多个对象。
例如:
class MyClass:
def __init__(self):
logging.basicConfig(filename='yaomaitong.log', filemode="w", level=logging.INFO) # 典型的反例 def run(self,*args):
# 所有需要多进程的程序都该写在这里
self.start()
self.continue()
self.end()
pass def start(self):
pass
...
线程池、进程池:
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor import requests urls_list = [
'https://www.baidu.com',
'http://www.gaosiedu.com',
'https://www.jd.com',
'https://www.taobao.com',
'https://news.baidu.com',
]
pool = ThreadPoolExecutor(3) def request(url):
response = requests.get(url)
return response def read_data(future,*args,**kwargs):
response = future.result()
response.encoding = 'utf-8'
print(response.status_code,response.url) def main():
for url in urls_list:
done = pool.submit(request,url)
done.add_done_callback(read_data) if __name__ == '__main__':
main()
pool.shutdown(wait=True)
python 线程,进程28原则的更多相关文章
- python线程进程
多道技术: 多道程序设计技术 所谓多道程序设计技术,就是指允许多个程序同时进入内存并运行.即同时把多个程序放入内存,并允许它们交替在CPU中运行,它们共享系统中的各种硬.软件资源.当一道程序因I/O请 ...
- python 线程 进程
1.进程与线程优.缺点的比较总言:使用进程和线程的目的,提高执行效率. 进程: 优点:能利用机器的多核性能,同时进行多个操作. 缺点:需要耗费资源,重新开辟内存空间,耗内存. 线程: 优点:共享内存( ...
- python 线程 进程 协程 学习
转载自大神博客:http://www.cnblogs.com/aylin/p/5601969.html 仅供学习使用···· python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件和 ...
- Python 线程&进程与协程
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- python 线程进程
一 线程的2种调用方式 直接调用 实例1: import threading import time def sayhi(num): #定义每个线程要运行的函数 print("runni ...
- python线程,进程,队列和缓存
一.线程 threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. 创建线程的两种方式1.threading.Thread import threading def f1(arg): ...
- python 线程/进程模块
线程的基本使用: import threading # ###################### 1.线程的基本使用 def func(arg): print(arg) t = threading ...
- python 线程 进程 标识
s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...
- python 线程(一)理论部分
Python线程 进程有很多优点,它提供了多道编程,可以提高计算机CPU的利用率.既然进程这么优秀,为什么还要线程呢?其实,仔细观察就会发现进程还是有很多缺陷的. 主要体现在一下几个方面: 进程只能在 ...
随机推荐
- vue-cli 3.0 axios 跨域请求代理配置及生产环境 baseUrl 配置
1. 开发环境跨域配置 在 vue.config.js 文件中: module.exports = { runtimeCompiler: true, publicPath: '/', // 设置打包文 ...
- 案例学Python--案例四:Django实现一个网站的雏形(2)
续上篇,用Django创建了一个Web,我们肯定想展示自己的页面,简单点,我们想看到自己的HelloWorld.此处要从项目的配置说起,方法和路径配对了,展现页面分分钟的事情. 先上效果图吧: ...
- Python:线程之定位与销毁
背景 开工前我就觉得有什么不太对劲,感觉要背锅.这可不,上班第三天就捅锅了. 我们有个了不起的后台程序,可以动态加载模块,并以线程方式运行,通过这种形式实现插件的功能.而模块更新时候,后台程序自身不会 ...
- C# 读取Json配置文件
今天需要用到读取Json配置文件的helper 结果竟然没找到合适的 微软自己有一个 不过不支持.Net fw 4.0 于是自己在NewTonSoft.Json的基础上 加了点小小的封装 ...
- LeetCode Pow(x, n) (快速幂)
题意 Implement pow(x, n). 求X的N次方. 解法 用正常的办法来做是会超时的,因为可能有21亿次方的情况,所以需要优化一下.这里用到了快速幂算法,简单来说就是将指数分解成二进制的形 ...
- [朴智妍][Lullaby]
歌词来源:http://music.163.com/#/song?id=484056971 作曲 : Bum/Sophiya/김용신 [作曲 : Bum/Sophiya/k/gi-myong-xin] ...
- 磁盘挂载问题:Fdisk最大只能创建2T分区的盘,超过2T使用parted
需求说明:云服务器上买了一块8T的磁盘,准备挂载到服务器上的/data目录下. ===================================parted命令说明=============== ...
- C_数据结构_递归不同函数间调用
# include <stdio.h> void f(); void g(); void k(); void f() { printf("FFFF\n"); g(); ...
- 不重叠的线段 51nod
链接 [http://www.51nod.com/onlineJudge/questionCode.html#problemId=1133¬iceId=468024] 题意 X轴上有N条 ...
- K 班前7次作业成绩汇总
K 班前7次作业成绩汇总 得分榜 千帆竞发 详细 短学号 名 1 2 3 4 5 6 7 TOTAL 505 基智 4.55 1 -2 0 0 -10 4.37 -2.08 414 圳源 5.43 2 ...