Python开发【笔记】:关于子线程(子进程)与主线程(主进程)的关联
前言:
主要分析下面的问题:
- 主线程启线程 主线程执行完毕,会关闭子线程吗?
- 子线程启线程 主线程执行完毕,会结束吗?
- 主进程启动进程,主进程执行完毕,会怎样?
- 子进程启动进程,进程执行完毕,又会如何?
import time
import threading def function():
time.sleep(2)
print('sub thread [%s] execute done' % threading.currentThread().ident) def main():
threading.Thread(target=function).start() print('main thread [%s] execute done'%threading.currentThread().ident) if __name__ == '__main__':
main() # main thread [11920] execute done
# sub thread [8876] execute done
主线程执行完毕,等待子线程执行;若想主线程执行完毕,直接退出,需设置守护线程
示例1.2
import time
import threading def function():
time.sleep(2)
print('sub thread [%s] execute done' % threading.currentThread().ident) def main():
t = threading.Thread(target=function)
t.setDaemon(True)
t.start()
print('main thread [%s] execute done'%threading.currentThread().ident) if __name__ == '__main__':
main() # main thread [3052] execute done
2、子线程启动线程
示例2.1
import time
import threading def function():
time.sleep(2)
threading.Thread(target=subfunction).start()
print('sub thread [%s] execute done' % threading.currentThread().ident) def subfunction():
time.sleep(2)
print('sub thread [%s] execute done' % threading.currentThread().ident) def main():
threading.Thread(target=function).start() print('main thread [%s] execute done'%threading.currentThread().ident) if __name__ == '__main__':
main() # main thread [2288] execute done
# sub thread [9556] execute done
# sub thread [12156] execute done
如示例1.1一致,主线程会等待子子线程执行完毕,然后关闭
3、主进程启动进程
示例2.1
import os
import time
import multiprocessing def function():
time.sleep(2)
print('sub process [%s] execute done' % os.getpid()) def main():
multiprocessing.Process(target=function).start()
print('main process [%s] execute done'%os.getpid()) if __name__ == '__main__':
main() # main process [5628] execute done
# sub process [11060] execute done
主进程会等待子进程执行完毕后关闭
4、子进程启动进程
示例4.1
import os
import time
import multiprocessing def function():
time.sleep(2)
print('sub process [%s] execute done' % os.getpid()) def main():
pid = os.fork()
print(pid)
if pid > 0:
return
multiprocessing.Process(target=function).start()
print('main process [%s] execute done'%os.getpid()) if __name__ == '__main__':
main() # 20533
# 0
# main process [20533] execute done
# sub process[20534] execute done
子进程会等待进程执行完毕后关闭
Python开发【笔记】:关于子线程(子进程)与主线程(主进程)的关联的更多相关文章
- Android子线程更新UI主线程方法之Handler
背景: 我们开发应用程序的时候,处于线程安全的原因子线程通常是不能直接更新主线程(UI线程)中的UI元素的,那么在Android开发中有几种方法解决这个问题,其中方法之一就是利用Handler处理的. ...
- Android:子线程向UI主线程发送消息
在Android里,UI线程是不同意被堵塞的.因此我们要将耗时的工作放到子线程中去处理. 那么子线程耗时处理后要如何通知UI线程呢? 我们能够在UI主线程中创建一个handler对象,然后通过重写其h ...
- 在C#中子线程如何操作主线程中窗体上控件
在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...
- python开发笔记-通过xml快捷获取数据
今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...
- Qt启动子进程,子进程关闭时通知主进程,实现主进程对子进程的管理
自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html 1.需求描述 Qt主进程启动 ...
- python开发_tkinter_多级子菜单
在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...
- Android开发之在子线程中使用Toast
在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...
- python开发笔记-python调用webservice接口
环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...
- 线程:Java主线程等待子线程结束
使用Thread.join()方法: public class App { public static void main(String[] args) { testMain(); } public ...
- C#子线程执行完后通知主线程
其实这个比较简单,子线程怎么通知主线程,就是让子线程做完了自己的事儿就去干主线程的转回去干主线程的事儿. 那么怎么让子线程去做主线程的事儿呢,我们只需要把主线程的方法传递给子线程就行了,那么传递方法就 ...
随机推荐
- Tomcat漏洞利用与安全加固实例分析
Tomcat中间件经常遇到的漏洞: 1.Tomcat默认存在一个管理后台,默认的管理地址是http://IP或域名:端口号/manager/html 2.Axis2默认口令安全漏洞,默认的管理地址是h ...
- [Ubuntu] arp-scan - 扫描网络设备
使用arp-scan扫描所有网络设备信息. 1. 安装arp-scan ifantastic@ubuntu:~$ sudo apt-get install arp-scan 2. 扫描网络所有设备 i ...
- C#实现新建文件并写入内容
using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- JS缺失错误- Uncaught SyntaxError: Unexpected token <
这种情况,表明,缺少js文件 解决方式:在文件夹下将缺少js文件补足
- linux批量修改文件名
源文件; [root@test_machine fuzj]# ls fuzj-1.txt fuzj-2.txt fuzj-3.txt fuzj-4.txt fuzj-5.txt fuzj-6 ...
- PHP关于=>和->以及::的用法
1.=>的用法 在php中数组默认键名是整数,也可以自己定义任意字符键名(最好是有实际意义),如: $css=array('style'=>'0',‘color’=>‘green‘) ...
- Python学习(20):Python函数(4):关于函数式编程的内建函数
转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.关于函数式编程的内建函数 apply()逐渐被舍弃,这里不讨论 1.filter() #filte ...
- js - 预加载+监听图片资源加载制作进度条
这两天遇到一个新需求:一个一镜到底的h5动画.因为功能的特殊性,就要求我们提前监听页面的静态图片是否全部加载完毕.即处理预加载. 总结下来,下次这种需求需要提前注意以下几点: 一.图片而不是背景图 本 ...
- C# EF中调用 存储过程并调回参数
TourEntities db = new TourEntities(); List<v_product> v = new List<v_product>(); SqlPara ...