前言:

主要分析下面的问题:

  • 主线程启线程  主线程执行完毕,会关闭子线程吗?
  • 子线程启线程  主线程执行完毕,会结束吗?
  • 主进程启动进程,主进程执行完毕,会怎样?
  • 子进程启动进程,进程执行完毕,又会如何?
1、主线程启线程
示例1.1
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开发【笔记】:关于子线程(子进程)与主线程(主进程)的关联的更多相关文章

  1. Android子线程更新UI主线程方法之Handler

    背景: 我们开发应用程序的时候,处于线程安全的原因子线程通常是不能直接更新主线程(UI线程)中的UI元素的,那么在Android开发中有几种方法解决这个问题,其中方法之一就是利用Handler处理的. ...

  2. Android:子线程向UI主线程发送消息

    在Android里,UI线程是不同意被堵塞的.因此我们要将耗时的工作放到子线程中去处理. 那么子线程耗时处理后要如何通知UI线程呢? 我们能够在UI主线程中创建一个handler对象,然后通过重写其h ...

  3. 在C#中子线程如何操作主线程中窗体上控件

    在C#中,直接在子线程中对窗体上的控件操作是会出现异常,这是由于子线程和运行窗体的线程是不同的空间,因此想要在子线程来操作窗体上的控件,是不可能 简单的通过控件对象名来操作,但不是说不能进行操作,微软 ...

  4. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  5. Qt启动子进程,子进程关闭时通知主进程,实现主进程对子进程的管理

    自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html 1.需求描述  Qt主进程启动 ...

  6. python开发_tkinter_多级子菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  7. Android开发之在子线程中使用Toast

    在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...

  8. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

  9. 线程:Java主线程等待子线程结束

    使用Thread.join()方法: public class App { public static void main(String[] args) { testMain(); } public ...

  10. C#子线程执行完后通知主线程

    其实这个比较简单,子线程怎么通知主线程,就是让子线程做完了自己的事儿就去干主线程的转回去干主线程的事儿. 那么怎么让子线程去做主线程的事儿呢,我们只需要把主线程的方法传递给子线程就行了,那么传递方法就 ...

随机推荐

  1. 【代码审计】eduaskcms_v1.0.7前台存储型XSS漏洞分析

      0x00 环境准备 eduaskcms官网:https://www.eduaskcms.xin 网站源码版本:eduaskcms-1.0.7 程序源码下载:https://www.eduaskcm ...

  2. python卸载或者安装时提示There is a problem with this Windows Installer package.A program required for this install to complete could not be run. Contact your support personnel or package vendor

    1.卸载时报这个错,先进行下修复,再执行卸载: 2.安装时报这个错,安装的过程中,没有取得管理员的权限. Msi格式的文件,点右键后,也没有“以管理员身份运行”的菜单项,那怎么办呢?你可以点“开始”菜 ...

  3. hadoop核心逻辑shuffle代码分析-map端

    首先要推荐一下:http://www.alidata.org/archives/1470 阿里的大牛在上面的文章中比较详细的介绍了shuffle过程中mapper和reduce的每个过程,强烈推荐先读 ...

  4. lua中的字符串操作(模式匹配)

    (一). 模式匹配函数在string库中功能最强大的函数是:string.find(字符串查找)string.gsub(全局字符串替换)string.gfind(全局字符串查找)string.gmat ...

  5. 《Lua程序设计》第2章 类型与值 学习笔记

    Lua中的8中基础类型:nil(空).boolean(布尔).number(数字).string(字符串).userdata(自定义类型).function(函数).thread(线程)和table( ...

  6. Charles抓包(iOS的http/https请求)

    Charles抓包(iOS的http/https请求) Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles:https://www.charlesp ...

  7. <转>github入门到上传本地项目

    转自 http://www.cnblogs.com/specter45/p/github.html GitHub是基于git实现的代码托管.git是目前最好用的版本控制系统了,非常受欢迎,比之svn更 ...

  8. SharpGL学习笔记(二) 模型变换(几何变换)

    (二) 模型变换 模形变换就是指的在世界坐标系中(world space)做“移动”,“旋转", "缩放"三种操作. 首先要说明的,在Opengl中,是用4x4矩阵进行坐 ...

  9. Bat脚本实现监控进程功能

    脚本不间断监控notepad.exe进程是否执行,若停止,则自动重启该进程,程序如下: @echo off set _task = notepad.exe set _svr = c:\windows\ ...

  10. 题目1076:N的阶乘(大数乘法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1076 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...