惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
量子位
A
About on SuperTechFans
The Register - Security
The Register - Security
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
Hacker News: Ask HN
Hacker News: Ask HN
T
The Exploit Database - CXSecurity.com
Vercel News
Vercel News
Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
NISL@THU
NISL@THU
V2EX - 技术
V2EX - 技术
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Schneier on Security
Schneier on Security
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
AWS News Blog
AWS News Blog
The GitHub Blog
The GitHub Blog
C
Cisco Blogs
T
Tenable Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Vulnerabilities – Threatpost
美团技术团队
L
LangChain Blog
Google DeepMind News
Google DeepMind News
腾讯CDC
P
Privacy International News Feed
Spread Privacy
Spread Privacy
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
S
Security @ Cisco Blogs

博客园 - 西康的博客

mysql information_schema库中COLUMNS 和 TABLES表字段详解 python - queue 队列模块 python - 修改系统环境变量 python - socketserver python - 反射 python - socket 基础 python 序列化 字典 异常处理 类的成员,类的特殊方法 elasticsearch RESTfull _cat api python - 面向对象编程基础知识 (进阶) python - 面向对象编程基础知识 python 文件操作 python re 模块 正则表达式 python configparser 模块 python logging 模块 python hashlib 模块 python sys 模块
python - threading 多进程模块
西康的博客 · 2018-04-25 · via 博客园 - 西康的博客

最基本的多进程示例:

import threading
import time

def bar(start_time,sleep_time):
    print ('is start_time: ......',start_time)
    time.sleep(sleep_time)
    print ('sleep done,',sleep_time)

if __name__ == '__main__':
    b1 = threading.Thread(target=bar,args=(time.time(),44))
    b2 = threading.Thread(target=bar,args=(time.time(),65))
    b1.start()
    b2.start()

    print ('script done!')

join:

  等待至线程中止。阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。

import threading
import time
def bar(name,sleep_time):
    print ('is start_time: ......',name)
    time.sleep(sleep_time)
    print ('sleep done,',sleep_time)
if __name__ == '__main__':
    b1 = threading.Thread(target=bar,args=('name_1',2))
    b2 = threading.Thread(target=bar,args=('name_2',3))

    b1.start()
    b2.start()
    b1.join()

    print ('script done!')

# is start_time: ...... name_1
# is start_time: ...... name_2
# sleep done, 2
# script done!
# sleep done, 3

setDaemon(daemonic) :

  设置守护线程标志为布尔值daemonic。它必须在start()调用之前被调用。

import threading
import time
def bar(name,sleep_time):
    print ('is start_time: ......',name)
    time.sleep(sleep_time)
    print ('sleep done,',name)
if __name__ == '__main__':
    threahs = list()

    b1 = threading.Thread(target=bar,args=('name_1',2))
    threahs.append(b1)
    b2 = threading.Thread(target=bar,args=('name_2',3))
    threahs.append(b2)

    b2.setDaemon(True)
    for i in threahs:
        i.start()

    print ('script done!')

# is start_time: ...... name_1
# is start_time: ...... name_2
# script done!
# sleep done, name_1

此处b2是daemon进程,所以在主进程运行完成时直接退出。不会去理会b2的进程是否执行完成。

getName()
  返回线程名。

setName(name)
  设置线程名。
  这名字是只用来进行标识目的的字符串。它没有其他作用。多个线程可以取同一名字。最初的名字通过构造函数设置。

isAlive()
  返回线程是否活动的。

isDaemon()
  返回线程的守护线程标志

run()
  用以表示线程活动的方法。你可能在Python Thread类的子类重写这方法。标准的 run()方法调用作为target传递给对象构造函数的回调对象。

并发&并行

并发:

  是指系统具有处理多个任务(动作)的能力

并行:

  是指系统具有同时处理多个任务(动作)的能力

同步&异步

同步:在发出一个功能调用时,在没有得到结果之前,该调用就不会返回,直到有返回值了才继续执行。

  类似打电话时,等待接电话的过程。

异步:在发出一个功能调用是,如果没有得到结果,则继续执行。一旦有返回结果了,可以再继续使用返回结果接着执行。

  类似发短信,不需要对方立即回复。即可继续做其他事情,当对方回复短信了,则再处理短信。