易截截图软件、单文件、免安装、纯绿色、仅160KB

Python 线程同步队列

我们经常会采用生产者/消费者关系的两个线程来处理一个共享缓冲区的数据。例如一个生产者线程接受用户数据放入一个共享缓冲区里,等待一个消费者线
程对数据取出处理。但是如果缓冲区的太小而生产者和消费者两个异步线程的速度不同时,容易出现一个线程等待另一个情况。为了尽可能的缩短共享资源并以相同
速度工作的各线程的等待时间,我们可以使用一个“队列”来提供额外的缓冲区。
创建一个“队列”对象
import Queue
myqueue = Queue.Queue(maxsize = 10)
Queue.Queue
类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue
的构造函数的可选参数maxsize
来设定队列长度。如果maxsize
小于1
就表示队列长度无限。
将一个值放入队列中
myqueue.put(10)
调用队列对象的put()
方法在队尾插入一个项目。put()
有两个参数,第一个item
为必需的,为插入项目的值;第二个block
为可选参数,默认为1
。如果队列当前为空且block
为1
,put()
方法就使调用线程暂停,直到空出一个数据单元。如果block
为0
,put
方法将引发Full
异常。
将一个值从队列中取出
myqueue.get()
调用队列对象的get()
方法从队头删除并返回一个项目。可选参数为block
,默认为1
。如果队列为空且block
为1
,get()
就使调用线程暂停,直至有项目可用。如果block
为0,队列将引发Empty
异常。
我们用一个例子来展示如何使用Queue
# queue_example.py
from Queue import Queue
import threading
import random
import time
# Producer thread
class Producer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'adding',i,'to queue'
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Consumer thread
class Consumer(threading.Thread):
def __init__(self, threadname, queue):
threading.Thread.__init__(self, name = threadname)
self.sharedata = queue
def run(self):
for i in range(20):
print self.getName(),'got a value:',self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'
# Main thread
def main():
queue = Queue(


相关文档:

python31与C交互

1.c调用python:
   实例代码:
main.c调用test.py的
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main.c
#include <windows.h>
......

python的C、c++扩展

python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 ......

python 的time模板翻译及说明

python 的内嵌time模板翻译及说明
一、简介
time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
year ......

python string和PyQt的QString的区别

python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for
Python2,6环境下讨论: Python中有两种有关字符的类型:Python string object和Python Unicode
object。主要使用Python string object进行数据输入输出。 PyQt中与之相对应的字符有关类
python string和PyQt的QString的区别
以下在Python2.6和PyQt4 ......

关于python unicode的实验

实验环境:windows xp + vim
文件:test.py。编码:ansi
我们的目标操作test.py中保存的非英文字母。
文件头的#encoding=utf8/gbk,这个是用来说明源文件的硬盘编码以便python识别[4]。
----------------------------------------------
输入:
x = '中文'
输出: 编译失败
编译时需要知道‘中文’的硬盘编 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号