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()
相关推荐
基于Python网络编程实现的多人聊天室项目源码+项目说明+超详细注释.zip
客户端使用wxPython构建界面并通过socket与服务器通信,支持多线程接收数据;服务器端具备启动、保存记录及
Python socket+线程池+wxPython:从零实现多人聊天室
socket是网络编程的基石,基于TCP的点对点通信特性决定了A客户端发送的消息默认无法被B客户端直接接收,而多人聊天室的核心正是服务器的广播机制。本文从最基础的socket bind/listen/accept流程讲起,逐步构建一个可运行的IM系统。针对多客户端并发接入,引入ThreadPoolExecutor线程池管理会话线程,避免频繁创建线程带来的资源开销;结合wxPython构建图形界面,通过wx.CallAfter解决网络线程与UI线程的同步问题,让收发数据与界面刷新互不阻塞。文章还深入讨论了广播
Python核心编程中文版 第2版和第3版
第4章:多线程编程多线程是处理并发任务的关键技术,本章会介绍Python的线程模型,包括`threading`模块的使用,如创建线程、线程同步(锁、信号量、事件等)、线程池和守护线程。
PythonApplications-SourceCode:它包含使用Python设计的真实世界应用程序文件-Source code world
**并发与多线程**:Python的threading和multiprocessing模块可以帮助开发者实现并发执行,提升程序效率。源代码可能包含线程池、信号量等同步原语的使用。8.
网络课程设计 -Echo程序的设计和实现
**图形用户界面**:GUI是用户与计算机系统进行交互的直观方式。在Echo程序中,可能采用了如Tkinter、Qt或wxPython等库来创建GUI,使用户能够方便地输入数据并查看服务器的回应。


