基于函数实现

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原则的更多相关文章

  1. python线程进程

    多道技术: 多道程序设计技术 所谓多道程序设计技术,就是指允许多个程序同时进入内存并运行.即同时把多个程序放入内存,并允许它们交替在CPU中运行,它们共享系统中的各种硬.软件资源.当一道程序因I/O请 ...

  2. python 线程 进程

    1.进程与线程优.缺点的比较总言:使用进程和线程的目的,提高执行效率. 进程: 优点:能利用机器的多核性能,同时进行多个操作. 缺点:需要耗费资源,重新开辟内存空间,耗内存. 线程: 优点:共享内存( ...

  3. python 线程 进程 协程 学习

    转载自大神博客:http://www.cnblogs.com/aylin/p/5601969.html 仅供学习使用···· python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件和 ...

  4. Python 线程&进程与协程

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  5. python 线程进程

      一 线程的2种调用方式 直接调用 实例1: import threading import time def sayhi(num): #定义每个线程要运行的函数 print("runni ...

  6. python线程,进程,队列和缓存

    一.线程 threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. 创建线程的两种方式1.threading.Thread import threading def f1(arg): ...

  7. python 线程/进程模块

    线程的基本使用: import threading # ###################### 1.线程的基本使用 def func(arg): print(arg) t = threading ...

  8. python 线程 进程 标识

    s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...

  9. python 线程(一)理论部分

    Python线程 进程有很多优点,它提供了多道编程,可以提高计算机CPU的利用率.既然进程这么优秀,为什么还要线程呢?其实,仔细观察就会发现进程还是有很多缺陷的. 主要体现在一下几个方面: 进程只能在 ...

随机推荐

  1. Codeforces Round #503 (by SIS, Div. 2)-C. Elections

    枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...

  2. C++高质量编程笔记

    /* * 函数介绍: * 输入参数: * 输出参数: * 返回值 : */ void Function(float x, float y, float z) { - } if (-) { - whil ...

  3. HDOJ1287_破译密码

    一道正常简单题 曲折解题 做这题的时候看了很久没有看懂是什么意思,最后以为是一道单独的数学题把B这个大写字母猜出来进行异或运算,还不知道C里面异或运算可以直接有符号的:),导致又去学习了一下十进制转换 ...

  4. Maven+SSM整合.doc

    Maven + SSM整合 1开发环境搭建 1.1Eclipse4.7(Oxygen) + Tomcat8 + Maven3.5.2 2Maven Web项目创建 2.1新建maven项目 2.2 选 ...

  5. 面象对象设计原则之三:里氏替换原则(The Liskov Substitution Principle,LSP)

    里氏代换原则由2008年图灵奖得主.美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing教授于1994年提出.其严格表述如下:如果对每一个类型为S的 ...

  6. SQLSERVER 2014 内存优化表相关

    更新了SP2的补丁能够解决  不能收缩日志文件的bug了. 但是因为已经不用内存优化表了, 所以想着能够删除内存优化表的file group 但是发现 很难删除 先说结论: 以下是针对内存优化文件组的 ...

  7. 【转帖】intel 2018年1 月2号爆出漏洞分析 知乎匿名用户

    作者:匿名用户链接:https://www.zhihu.com/question/265012502/answer/288407097来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  8. BOM嵌套简单写法

    WITHTREE AS( SELECT a.FItemID cfitemid,b.FItemID pfitemid FROM dbo.ICBOMChild a,dbo.ICBOM b WHERE a. ...

  9. 序列化与反序列化,json,pickle,xml,shelve,configparser模块

    序列化与反序列化 什么是序列化?序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输.反序列化就是将硬盘中或者网络中传来的一种数据格式转换成内存中数据结构. 为什么要有? 1.可以 ...

  10. 如何实现圆形的进度条(ProgressBar)

    在我们实际的工作中可能经常使用到圆形的进度条,但是这是怎么实现的呢?其实这只不过是修改了一下ProgressBar的模板,我们在下面的代码中我们将ProgressBar的Value值绑定到Border ...