前言:

主要分析下面的问题:

  • 主线程启线程  主线程执行完毕,会关闭子线程吗?
  • 子线程启线程  主线程执行完毕,会结束吗?
  • 主进程启动进程,主进程执行完毕,会怎样?
  • 子进程启动进程,进程执行完毕,又会如何?
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. 【代码审计】CmsEasy_v5.7 代码执行漏洞分析

      0x00 环境准备 CmsEasy官网:http://www.cmseasy.cn/ 网站源码版本:CmsEasy_v5.7_UTF8-0208 程序源码下载: http://ftp.cmseas ...

  2. 利用Squid反向代理搭建CDN缓存服务器加快Web访问速度

    2011年11月26日 ? Web服务器架构 ? 评论数 2 案例:Web服务器:域名www.abc.com IP:192.168.21.129 电信单线路接入访问用户:电信宽带用户.移动宽带用户出现 ...

  3. C++ template —— 动多态与静多态(六)

    前面的几篇博文介绍了模板的基础知识,并且也深入的讲解了模板的特性.接下来的博文中,将会针对模板与设计进行相关的介绍.------------------------------------------ ...

  4. STL——关联式容器

    一.关联式容器 标准的STL关联式容器分为set(集合)/map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和 multimap(多键映射表).这些容器的底层机制均以RB-tre ...

  5. IDA + VMware 调试win7 x64

    IDA+gdb配合VMware调试windows已经不是什么新鲜事了,但是之所以要发这篇帖子是因为我按照之前的帖子还有网上其他的教程设置调试环境,结果遇到了各种问题,所以仅仅是更新一下,各位轻拍. 环 ...

  6. 获取对象属性值=NPOI EXPORT

    使用dll ==== NPOI.dll 获取属性,设置属性=参考:http://blog.csdn.net/cestarme/article/details/6548126 额外的: 导出的时候碰到一 ...

  7. JS 运行、复制、另存为 代码。

    //运行代码 function runEx(cod1) { cod = document.getElementById(cod1) var code = cod.value; if (code != ...

  8. Undeclared identifier:XXX

    未识别错误,是因拼写错误或找不到定义文件. 下面列举一些类型和函数用到的单元. _Stream ADODB_TLB akTop, akLeft, akRight, akBottom Controls ...

  9. Android Studio 3.0.1 版本包下载

    Android Studio 3.0.1 发布了,这是对 Android Studio 3.0 的一个小的更新,包括一般错误修复和性能改进 下载地址: Windows 64 位:https://dl. ...

  10. sencha touch 常见问题解答(26-50)

    26.sencha touch在华为.红米等部分手机下hide事件失效,msgbox无法关闭怎么办 答:请看http://www.cnblogs.com/cjpx00008/p/3535557.htm ...