七日Python之路--第四天(之多线程)
时间:2022-07-22
本文章向大家介绍七日Python之路--第四天(之多线程),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
(一)程序,进程,线程
程序,为解决某特定问题而用计算机语言编写的命令序列的集合。静态,一个程序可以有多个进程。
进程,是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。动态,程序的动态运行。
线程,是进程中某个单一的顺序的控制。
在单个程序中,同时运行多个线程完成不同的工作,称为多线程。
# 一段简单的代码
#!/usr/bin/python
#coding:utf-8
import time
def lop(name,times=2,sleeptime=0):
for i in xrange(times):
time.sleep(sleeptime)
print '%s:%s' % (name,i)
lop('a',3)
lop('b',3)
python中,拥有众多的类库。对于线程,需要使用到Python中thread模块。
(二)线程锁
线程的运行:任意时刻只能有一个线程在执行。
thread模块:可在Python中,使用help(thread)查看帮助
#!/usr/bin/python
#coding:utf-8
import time
import thread
def lop(name,times,sleeptime,l):
for i in xrange(times):
time.sleep(sleeptime)
print '%s:%s' % (name,i)
#进行解锁
l.release()
#生成一个线程锁
lock = thread.allocate_lock()
#进行加锁
lock.acquire()
#新建一个线程
thread.start_new_thread(lop,('a',5,0,lock))
#当线程锁处于加锁状态时,进程继续执行
while lock.locked():
pass
注:程序代码在开始执行时,便形成了一个进程。或者说进程就是程序代码的一次动态执行过程。当执行到thread.start_new_thread()时,便新建了一个线程,然后由线程去执行lop()函数。此时进程继续执行,倘若不加线程锁,刚才新建的线程会随着该进程的结束而结束,可能会导致线程没执行完就意外终止。
thread只是Python中提供的一个对线程简单的控制。更复杂的控制需要使用,threading模块。
thread.start_new_thread(function, args[, kwargs])
Start a new thread and return its identifier. The thread executes the functionfunction with the argument list args (which must be a tuple). The optionalkwargs argument specifies a dictionary of keyword arguments. When the function
returns, the thread silently exits. When the function terminates with an
unhandled exception, a stack trace is printed and then the thread exits (but
other threads continue to run).
thread.allocate_lock()
Return a new lock object. Methods of locks are described below. The lock is
initially unlocked.
lock.acquire([waitflag])
Without the optional argument, this method acquires the lock unconditionally, if
necessary waiting until it is released by another thread (only one thread at a
time can acquire a lock — that’s their reason for existence). If the integerwaitflag argument is present, the action depends on its value: if it is zero,
the lock is only acquired if it can be acquired immediately without waiting,
while if it is nonzero, the lock is acquired unconditionally as before. The
return value is True if the lock is acquired successfully, False if not.
lock.release()
Releases the lock. The lock must have been acquired earlier, but not
necessarily by the same thread.
lock.locked()
Return the status of the lock: True if it has been acquired by some thread,False if not.
关于线程的知识点比较多......Orz.
稍后再做研究,先看Django去了 ......
--2014.7.24 15:17
- 深入理解C# 3.x的新特性(2):Extension Method[下篇]
- 19.13/19.14 配置邮件告警
- 19.12 添加自定义监控项目
- [原创]深入理解C# 3.x的新特性(3):从Delegate、Anonymous Method到Lambda Expression
- a=b=c,但a不等于c?这是什么鬼
- 为什么不是0,1,2,3,4?setTimeout的奇怪问题
- Linux基础(day67)
- 深入理解C# 3.x的新特性(5):Object Initializer 和 Collection Initializer
- 19.10 处理图形中的乱码
- 《深入理解C# 3.x的新特性》博文系列汇总
- 十一国庆节 之 “变量与函数同名时,会输出谁?”
- 挖坑无止境,来看看这个《this的指向》
- T-SQL Enhancement in SQL Server 2005[上篇]
- 初学js钻太深,不太好
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- String - 161. One Edit Distance
- String - 6. ZigZag Conversion
- String - 179. Largest Number
- 利用Github Pages实现“服务CNAME”功能
- String - 49. Group Anagrams
- Flutter包大小治理上的探索与实践
- String - 242. Valid Anagram
- String - 290. Word Pattern
- String - 205. Isomorphic Strings
- String - 345. Reverse Vowels of a String
- String - 186. Reverse Words in a String II
- String - 151. Reverse Words in a String
- String - 344. Reverse String
- String - 383. Ransom Note
- String - 58. Length of Last Word