wxpython与线程池

lioujian47 2009-05-29 12:45:40
这是一个简化的版本,主要是不知道如何将线程池中的内容传入wxpython依次显示出来,请指教.谢谢先!



#-*- coding: UTF-8 -*-

import Queue
import threading
import urllib2
import wx

hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
"http://ibm.com", "http://apple.com"]

queue = Queue.Queue()
text_1 = []
text_2 = []
j = 0
for i in hosts:
text_1.append('self.txt1_%d'%j)
text_2.append('self.txt2_%d'%j)

class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "编辑器")
self.panel = wx.ScrolledWindow(self, -1, style=wx.VSCROLL)
self.panel.SetScrollbars(1, 1, 1, 300)
sizer = wx.GridBagSizer()
j = 0
for i in hosts:
text_1[j] = wx.TextCtrl(self.panel, -1,"", size=(180,20))
text_2[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(180,90))
sizer.Add(text_1[j], pos=( 2*j, 0 ))
sizer.Add(text_2[j], pos=( 2*j + 1, 0 ))
j = j + 1
self.panel.SetSizer(sizer)
self.panel.Fit()
main()
def Message(self, text): # 写到这里我就不知道怎么办了:(
# 目的是将线程池产生的head, html在text_1[0], text_1[1], text_2[0], text_2[1]
# 中显示出来.当然如果您有更好的办法可以说说:)


class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue, window):
threading.Thread.__init__(self)
self.queue = queue
self.window = window

def run(self):
while True:
#grabs host from queue
host = self.queue.get()

#grabs urls of hosts and prints first 1024 bytes of page
url = urllib2.urlopen(host)
head = url.read(1024)
html = url.read()

#signals to queue job is done
self.queue.task_done()
wx.CallAfter(self.window.Message, head, html) #通过CallAfter将head, html传入text1, text2

def main():
#spawn a pool of threads, and pass them queue instance
for i in range(5):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()

#populate queue with data
for host in hosts:
queue.put(host)

#wait on the queue until everything has been processed
queue.join()

app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()




...全文
285 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lioujian47 2009-06-01
  • 打赏
  • 举报
回复
的确,我省略了很多.下面是一个可以运行而且是期望的结果的代码,不过,这仅仅是在得到内容比较少的时候可以成功的.用heads.append()和htmls.append()的办法收集queue的办法是不可取的,因为,当再来几个Queue队列的时候,就一片混乱了.


#-*- coding: UTF-8 -*-

import Queue
import threading
import urllib2
import wx

hosts = ["http://yahoo.com", "http://google.com",
"http://ibm.com", "http://apple.com"]

he = []
ht = []
n = 0

queue = Queue.Queue()
text_1 = []
text_2 = []
j = 0

for i in range(4):
text_1.append('self.txt1_%d'%j)
text_2.append('self.txt2_%d'%j)

class WorkerThread(threading.Thread):
def __init__(self, window):
threading.Thread.__init__(self)
self.window = window

def run(self):
#print '--------------'
wx.CallAfter(self.window.Message, he, ht)

class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "编辑器")
self.panel = wx.ScrolledWindow(self, -1, style=wx.VSCROLL)
self.panel.SetScrollbars(1, 1, 1, 300)
sizer = wx.GridBagSizer()
j = 0
for i in hosts:
text_1[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(200,100))
text_2[j] = wx.TextCtrl(self.panel, -1,"",
style=wx.TE_MULTILINE|wx.TE_RICH2, size=(200,180))
sizer.Add(text_1[j], pos=( 2*j , 0 ))
sizer.Add(text_2[j], pos=( 2*j , 1 ))
j = j + 1
#print j # j = 4
self.panel.SetSizer(sizer)
self.panel.Fit()
main()

class ThreadUrl(threading.Thread):
"""Threaded Url Grab"""
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#grabs host from queue
host = self.queue.get()
#grabs urls of hosts and prints first 1024 bytes of page
url = urllib2.urlopen(host)
head = url.read(1024)
html = url.read()
he.append(head)
ht.append(html)
if len(he) == number and len(ht)== number:
thread = WorkerThread(self)
thread.start()
#signals to queue job is done
self.queue.task_done()
def Message(self, he, ht):
for i in range(4):
#for i in number: #用这个循环要报错 number = len(hosts)
text_1[i].AppendText(he[i])
text_2[i].AppendText(ht[i])

def main():
#spawn a pool of threads, and pass them queue instance
for i in range(4):
t = ThreadUrl(queue)
t.setDaemon(True)
t.start()
#populate queue with data
for host in hosts:
queue.put(host)
#wait on the queue until everything has been processed
queue.join()

app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()


lioujian47 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fairchild811 的回复:]
heads.append(head) 是什么意思?
[/Quote]
程序的目的:
抓取网页,筛取需要的内容,在wxpython中呈现出来.

上面的程序中:抓取 hosts中每个网站的 head = url.read(1024), html = url.read(),然后分别填入图形界面.
当抓到内容之后有2种处理方式,如下:
1.
将抓取的每个网站的head和html分别存入heads, htmls ( heads和htmls都是全局的[],这样做肯定有问题,同步要出问题),然后在wxpython一次性生成.这个方法肯定不可取.上面的代码就是这样.比如不是4个网站,可能40个,100个的时候,可能会产生同步等错误.

2.抓取一个网站的head和html就呈现一个到wxpython,不过这样的话,wxpython好像要用到refresh(),触发事件之类,麻烦一点,但可能效率更高.但这样的话,wxpython怎么写我就不知道了:(

完善的话用第2种办法,但如果refresh(),触发事件等还得请高手指教了:)



iambic 2009-05-31
  • 打赏
  • 举报
回复
你的代码运行不起来:ThreadUrl(queue)少一个window参数。
楼主改下基本能运行了再发上来吧……

你的main()写的也很奇怪,一点都不像main-_-!
fairchild811 2009-05-31
  • 打赏
  • 举报
回复
heads.append(head) 是什么意思?
lioujian47 2009-05-31
  • 打赏
  • 举报
回复
这个与wxpython可以无关的,只要queue的数据 能顺序的收集起来就可以传入wxpython,不过建立一个list,如heads.append(head)这样会导致错误发生.尤其在数据多的时候发生错误:(
困扰我一星期了..
lixq2000 2009-05-31
  • 打赏
  • 举报
回复
不懂wxpython,关注学习
lioujian47 2009-05-31
  • 打赏
  • 举报
回复
iambic,能不能提供点思路呀?指点一下吧?
lioujian47 2009-05-31
  • 打赏
  • 举报
回复
还是没有人指点一二吗?
baobao04551 2009-05-30
  • 打赏
  • 举报
回复
这个。。。真不会。。
lioujian47 2009-05-29
  • 打赏
  • 举报
回复
额...
队列:)
iambic 2009-05-29
  • 打赏
  • 举报
回复
管queue叫线程池?

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧