前言:

主要分析下面的问题:

  • 主线程启线程  主线程执行完毕,会关闭子线程吗?
  • 子线程启线程  主线程执行完毕,会结束吗?
  • 主进程启动进程,主进程执行完毕,会怎样?
  • 子进程启动进程,进程执行完毕,又会如何?
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. oracle URL参数获取

    改函数主要是从URL中获取参数例如 sssss.html?cur=aaa&ref=cccc 调用方式:f_querystr(url,'cur','&'); CREATE OR REPL ...

  2. 【LeetCode OJ】Search Insert Position

    题目:Given a sorted array and a target value, return the index if the target is found. If not, return ...

  3. 转:桩模块 stub 和驱动模块 driver

    迷惑我很久的stub的概念,今天终于看到觉得靠谱的了,原文地址:http://xyzhaoangela.blog.hexun.com/14208786_d.html 桩模块stub:集成测试前要为被测 ...

  4. 【ipad神坑】ipad麦克风听不到声音怎么回事 微信QQ语音视频对方都听不到

    今天遇到了这个问题 说话听不见,但是敲击ipad,可以明显的听到击打的声音 siri也是可以听到 上网上找,大多都是说恢复设置,重启,隐私麦克风权限等解决方案 都是浪费感情 全部尝试过了,依然没有用. ...

  5. |和||、&&和&

    |和||.&&和& | : 会检查每一个 条件的真伪,再做“或”运算 ||: 按照条件写的顺序,直到一个为true时,后面的条件则不再检查,直接进入条件 & : 会检查 ...

  6. jQuery事件处理(四)

    看了几天,决定整理一下jQuery事件处理的整体设计思路 1.通过add方法给选中的元素注册事件处理程序(通过缓存系统将事件储存到cache,而不是绑定到元素上) a.在存储之前,会为事件处理程序增加 ...

  7. sencha touch 在线实战培训 第一期 第一节

    经过忙碌的准备,终于在2013.12.28晚上8点开了第一节课. 第一次讲课有些小紧张,讲的内容也比较基础,不过算是开了一个好头. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内 ...

  8. 微信小游戏 交互接口的使用 wx.showToast wx.showLoading

    在小游戏中,会有如下图的提示窗口,这些可以使用微信提供的交互接口实现. 使用loading等待的接口.mask=true表示遮罩,防止等待时点击其他按钮触发其他操作导致异常. wx.showLoadi ...

  9. C# 批量上传

    完整例子下载 效果: 前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="d ...

  10. 【CF891C】Envy 离线+最小生成树

    [CF891C]Envy 题意:给你一个图,边有边权,每次询问给你一堆边,问你是否存在一个原图的最小生成树包含给出的所有边.n,m,q<=100000 题解:思路很好的题. 首先有一个非常重要的 ...