ChinaUnix.net
相关文章推荐:

python 线程间通信

作者: pascal4123 出处: pascal4123.cublog.cn (转载敬请注明作者或出处) 当脚本产生大量线时,很自然地,线会产生通信(改变和访问共享全局内存)的需求。有些情况需要小心对待,使用锁来同步对共享对象的访问。但这是一种比较老实的通信模式。 当脚本启动进序时,事情并没有这么简单。如果我们限制通信的类型,有许多手段。比如: 1. 命令行参数 2. 标准流重定向 3. os.popen调用产生的管道 4. 序退...

by pascal4123 - Python文档中心 - 2010-12-27 09:36:48 阅读(12878) 回复(3)

相关讨论

from multiprocessing import Process, Pipe from multiprocessing import Queue import time results = Queue() def f(conn): conn.send([42, None, 'hello']) conn.close() def lsp_send(queue): while True: temp=queue.get() if temp is not None: print temp def lsp(queue): while True: queue.put('hello word') time.sleep(100) if __name__ ==...

by luoshiping523 - Python - 2014-07-17 16:22:13 阅读(4108) 回复(7)

python中如何实现进通信? 例如现在有一个receiver.py来负责从标准输入读入信息,然后显示的屏幕上。 然后有一个transimitter.py来负责启动receiver.py并且向其发送信息。 在网上查到用subprocess这个模块,不过当调用subproc.stdin.write('info')的时候会抛出IOError 32 Broken PIPE错误,不知道为何。下面是代码: receiver.py: [code] import sys info = sys.stdin.readline() print info [/code] transimitter.py:...

by feiyang21687 - Python - 2007-08-17 13:29:30 阅读(8139) 回复(6)

在IBM网站里找到了一个python 线池的例子。 我在执行这个例子,序却挂起了。哪位大侠能帮忙看看到底是怎么回事吗? import Queue import threading import urllib2 import time hosts = ["http://www.sina.com.cn/", "http://www.163.com/", "http://www.sohu.com/","http://ibm.com.cn", "http://apple.com.cn"] queue =Queue.Queue(0) class ThreadUrl(threading.Thread): #threaded url Grab def __init__(self,que...

by xusancu - Python - 2012-04-11 22:13:10 阅读(3439) 回复(9)

最近在写一个爬虫,用到多线,定义一个队列url_queue,所有的线都从这个队列获得url,然后下载页面,解析页面,获得页面中的url,然后把这些获得的url推入url_queue中,我设想线的停止条件应该是所有的子线都暂停了,而且url_queue为空,可是这个停止条件我看threading模块却不知道如何实现,threading模块的join不行,因为我的子线都是while True循环,而Queue的join似乎也不行,我翻了下Queue的源码,发现有个unfinish...

by yy1990cn - Python - 2012-04-19 18:16:03 阅读(2414) 回复(6)

实现代码: #coding:utf-8 import Queue import threading import sys import time import urllib #替我们工作的线池中的线 class MyThread(threading.Thread): def __init__(self, workQueue, resultQueue,timeout=30, **kwargs): threading.Thread.__init__(self, kwargs=kwargs) #线在结束前等待任务队列多长时 self.timeout = timeout self.setDaemon(True) self.workQueue = workQueue self.resultQueu...

by hkebao - Python文档中心 - 2009-08-21 15:35:15 阅读(1423) 回复(0)

摘自:http://blog.niwota.com/rss/woblog C++线中嵌入python,线并不是安全的,需要锁机制保证线安全,我试了2种锁机制,一种是C++线Mutex锁,另一种是python本身的线锁 1、C++线Mutex锁: 加锁的Code #ifdef WIN32 EnterCriticalSection((CRITICAL_SECTION *)_mutex); #else pthread_mutex_lock(&_mutex); #endif 解锁的Code #ifdef WIN32 LeaveCriticalSection((CRITICAL_SECTION *)_mutex); #else pthread_mute...

by travelsky2008 - Python文档中心 - 2008-04-18 00:16:38 阅读(1480) 回复(0)

[code] import threading,urllib def openurl(): while 1: url = urllib.urlopen('http://domain.com') if __name__=='__main__': for i in range(100): i = threading.Thread(target = openurl) i.start() [/code] 写着玩的.

by tmdxy - Python - 2005-06-06 10:07:31 阅读(7017) 回复(0)

即其中一个线中有时钟定时,怎么才能在另一个线中提取这个时钟进行应用。 这个时钟是第一个线接受相关信息获取的,所以不能重复定义。

pythonthreading时钟线程

by moonlightcome - Python - 2010-06-29 14:40:49 阅读(2642) 回复(2)

主进创建一个子进p,对一些数据进行处理, 然后再创建一个线th,主要负责监控一个外部条件是否满足,如果条件满足,则通过terminate结束子进p; 子进p退出后,如何退出线th?试了用join没有达到预期效果 这种处理方式有问题么? 代码逻辑如下: ################### global_p = 0 test(): ...... while True: if(condition == True): global_p.terminate() ...... if __name__ == "__main__": ...... while True: ...

by howema - Python - 2012-11-20 18:04:26 阅读(6662) 回复(2)

本帖最后由 壮乡小顽主 于 2012-08-15 16:50 编辑 python核心编书上的脚本如下: import thread from time import ctime, sleep loops = [4, 2] def loop(nloop, nsec, lock): print 'start loop', nloop, 'at:', ctime() sleep(nsec) print 'loop', nloop, 'done at:', ctime() lock.release() def main(): print 'starting at:', ctime() locks = [] nloops = range(len(loops)) ...

by 壮乡小顽主 - Python - 2012-08-16 08:49:00 阅读(1306) 回复(6)