并发编程 - 线程 - 1.开启线程的两种方式/2.进程与线程的区别/3.Thread对象的其他属性或方法/4.守护线程
1.开启线程的两种方式:
进程,线程:
进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位)
1.同一个进程内的多个线程共享该进程内的地址资源
2.创建线程的开销远小于创建进程的开销(创建一个进程,就是创建一个车间,涉及到申请空间,
而且在该空间内建至少一条流水线,但创建线程,就只是在一个车间内造一条流水线,无需申请空间,所以创建开销小)
# 方式1:
from threading import Thread
import time def sayhi(name):
time.sleep(2)
print('%s say hello'%name) if __name__ == "__main__":
t=Thread(target=sayhi,args=('alice',))
t.start()
print('主线程') # 一个进程,一个主线程,开启了一个线程 # 方式2:
from threading import Thread
import time class Sayhi(Thread):
def __init__(self,name):
super().__init__()
self.name = name def run(self):
time.sleep(2)
print('%s say hello'%self.name) if __name__ == "__main__":
t = Sayhi('alice')
t.start()
print('主线程')
2.进程与线程的区别:
1.开进程的开销远大于开线程
2.同一进程内的多个线程共享该进程的地址空间
3.瞅一眼pid
# 1.
import time
from threading import Thread
from multiprocessing import Process def piao(name):
print('%s piaoing' %name)
time.sleep(2)
print('%s piao end' %name) if __name__ == '__main__':
# p1=Process(target=piao,args=('egon',))
# p1.start() # 发出去后,没有立刻反应 开进程开销大 t1=Thread(target=piao,args=('egon',))
t1.start() # 发出去后,立刻就反应了,开线程开销小 print('主线程')
"""
主线程
egon piaoing
egon piao end
"""
"""
egon piaoing
主线程
egon piao end
"""
# 2.
from threading import Thread
from multiprocessing import Process n=100
def task():
global n
n=0 if __name__ == '__main__':
# p1=Process(target=task)
# p1.start()
# p1.join() # 彼此隔离的 n = 100 t1=Thread(target=task)
t1.start()
t1.join() # 共享地址空间 n = 0 print('主线程',n)
"""
主线程 100
"""
"""
主线程 0
"""
# 3.
from threading import Thread
from multiprocessing import Process,current_process
import os def task():
print(current_process().pid)
print(os.getpid(),os.getppid()) if __name__ == '__main__':
# p1=Process(target=task)
# p1.start() # pid 不一样
# p1.join() t1=Thread(target=task)
t1.start() # 线程pid 一样
t1.join() print('主线程',current_process().pid)
print('主线程',os.getpid(),os.getppid())
"""
38780
38780 43168
主线程 43168
主线程 43168 12480
"""
"""
58912
58912 12480
主线程 58912
主线程 58912 12480
"""
3.Thread对象的其他属性或方法:
currentThread().getName()
t.setName()
t.is_alive()
t.isAlive()
t.join()
active_count()
enumerate()
from threading import Thread,currentThread,active_count,enumerate
import time def task():
print('%s is running'%currentThread().getName())
time.sleep(2)
print('%s is done'%currentThread().getName()) if __name__ == "__main__":
t = Thread(target=task,name='子线程1')
t.start()
# t.join()
# print(t.getName())
# t.setName('儿子线程1')
# currentThread().setName('主进程')
# print(t.is_alive())
# print(t.isAlive())
# print('主线程:',currentThread().getName())
t.join()
# print(active_count()) # 2 没有join 1 有join
print(enumerate()) # [<_MainThread(MainThread, started 41144)>] """
Thread-1 is running
Thread-1
主线程 MainThread
Thread-1 is done
"""
"""
子线程1 is running
子线程1
主线程 MainThread
子线程1 is done
"""
"""
子线程1 is running
主线程 MainThread
儿子线程1 is done
"""
"""
子线程1 is running
主线程: 主进程
子线程1 is done
"""
"""
子线程1 is running
子线程1 is done
False
False
主线程: MainThread
"""
4.守护线程:
一个进程什么时候被销毁,
1.不开线程,代码完毕,该进程就被销毁
2.主线程等到所有子线程死了后,他才死
守护线程等到所有的子线程死了后,他才死
from threading import Thread
import time def sayhi(name):
time.sleep(2)
print('%s say hello' %name) if __name__ == '__main__':
t=Thread(target=sayhi,args=('egon',))
# t.setDaemon(True) #必须在t.start()之前设置
t.daemon=True
t.start() print('主线程')
print(t.is_alive()) # t 跟着死了 一个线程内没有开启非守护线程
"""
主线程
True
""" from threading import Thread
import time def foo():
print(123)
time.sleep(4)
print("end123") def bar():
print(456)
time.sleep(3)
print("end456") if __name__ == '__main__':
t1=Thread(target=foo)
t2=Thread(target=bar) t1.daemon=True
t1.start()
t2.start()
print("main-------")
"""
123 foo time.sleep(1)
456
main-------
end123
end456
"""
"""
123 foo time.sleep(4)
456
main-------
end456
"""
并发编程 - 线程 - 1.开启线程的两种方式/2.进程与线程的区别/3.Thread对象的其他属性或方法/4.守护线程的更多相关文章
- python 之 并发编程(开启子进程的两种方式,进程对象的属性)
第九章并发编程 同一个程序执行多次是多个进程 import time import os print('爹是:',os.getppid()) #父进程PID,(pycharm) print('me ...
- 线程池提交任务的两种方式:execute与submit的区别
Java中的线程池在进行任务提交时,有两种方式:execute和submit方法. 一.execute和submit的区别 execute只能提交Runnable类型的任务,无返回值.submit既可 ...
- 8 并发编程-(线程)-多线程与多进程的区别&Thread对象的其他属性或方法
1.开启速度 在主进程下开启线程比 开启子进程快 # 1 在 主进程下开启线程 from threading import Thread def work(): print('hello') if ...
- 并发编程 - 进程 - 1.开启子进程的两种方式/2.查看pid/3.Process对象的其他属性或方法/4.守护进程
1.开启子进程的两种方式: # 方式1: from multiprocessing import Process import time def task(name): print('%s is ru ...
- [操作系统知识储备,进程相关概念,开启进程的两种方式、 进程Queue介绍]
[操作系统知识储备,进程相关概念,开启进程的两种方式.进程Queue介绍] 操作系统知识回顾 为什么要有操作系统. 程序员无法把所有的硬件操作细节都了解到,管理这些硬件并且加以优化使用是非常繁琐的工作 ...
- DJango中开启事务的两种方式
目录 Django中开启事务的两种方式 第一种 第二种 Django中开启事务的两种方式 第一种 from django.db import transaction with transaction. ...
- python 之 并发编程(线程理论,开启线程的两种方式,进程与线程的区别,线程对象的其他方法)
9.9 线程理论 1.什么是线程 线程指的是一条流水线的工作过程 进程根本就不是一个执行单位,进程其实是一个资源单位,一个进程内自带一个线程,线程才是执行单位 2.进程VS线程 同一进程内的线程们共享 ...
- 子进程回收资源两种方式,僵尸进程与孤儿进程,守护进程,进程间数据隔离,进程互斥锁,队列,IPC机制,线程,守护线程,线程池,回调函数add_done_callback,TCP服务端实现并发
子进程回收资源两种方式 - 1) join让主进程等待子进程结束,并回收子进程资源,主进程再结束并回收资源. - 2) 主进程 “正常结束” ,子进程与主进程一并被回收资源. from multipr ...
- Java并发编程:Java实现多线程的几种方式
在Java中,多线程主要的实现方式有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService ...
随机推荐
- lzma解压
这个软件的使用方法有点特殊:需要将要压缩为lzma格式的文件拖放到批处理上面,会自动进行处理.压缩和解压同样是拖放到上面,程序会自动处理.程序默认使用2个CPU线程进行处理,会自动判断你是压缩还是解压 ...
- Unix系统编程()发送信号的其他方式:raise和killpg
有时,进程需要向自身发送信号,raise 函数就执行了这一任务. #include <signal.h> int raise(int sig); 在单线程程序中,调用raise相当于对ki ...
- inline-block元素的空白间距解决方法<转>
使用inline-block来代替float进行布局,或者使用inline-block来实现元素的居中效果.有关于使用inline-block来代替float的讨论也蛮多的. 不过就是使用inline ...
- PostgreSQL视频去重 图片去重系列1
PostgreSQL 在视频.图片去重,图像搜索业务中的应用 图片搜索 PostgreSQL的图像搜索插件使用了非常主流的Haar wavelet技术对图像进行变换后存储 gist 索引方法(支持pa ...
- Oracle 错误:ORA-06413: Connection not open 解决办法
http://blog.csdn.net/neso520/article/details/6037411 ——————————————————————————————————————————————— ...
- 关于SQL高量问题
一工作今天在用DataTable.Table.Select("字段 like")查询时候老是碰到格式不正确 dtrFoundRow = dtvOOView.Table.Select ...
- tp 批量转码
读取王正东成功,然后把乱码一条一条的改回来... 专门针对mssql数据库的!!!
- before伪类的超有用应用技巧——水平菜单竖线分隔符
方法一.li前面加before伪类 <!doctype html> <html dir="ltr" lang="zh-CN"> < ...
- NYOJ 1009 So Easy[Ⅰ]【简单题】
/* 题目大意:求三角形的外接圆 解题思路:c/sin(C)=2R,先求出cos,在求出sin 关键点:直接调用库 解题人:lingnichong 解题时间:2014-10-18 10:19:33 解 ...
- [转]这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...