473,715 Members | 5,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython and threads

I'm writing a search engine in Python with wxPython as the GUI. I have
the actual searching preformed on a different thread from Gui thread.
It sends it's results through a Queue to the results ListCtrl which
adds a new item. This works fine or small searches, but when the
results number in the hundreds, the GUI is frozen for the duration of
the search. I suspect that so many search results are coming in that
the GUI thread is too busy updating lists to respond to events. I've
tried buffer the results so there's 20 results before they're sent to
the GUI thread and buffer them so the results are sent every .1
seconds. Nothing helps. Any advice would be great.

Jul 18 '07 #1
5 2606
Benjamin wrote:
I'm writing a search engine in Python with wxPython as the GUI. I have
the actual searching preformed on a different thread from Gui thread.
It sends it's results through a Queue to the results ListCtrl which
adds a new item. This works fine or small searches, but when the
results number in the hundreds, the GUI is frozen for the duration of
the search. I suspect that so many search results are coming in that
the GUI thread is too busy updating lists to respond to events. I've
tried buffer the results so there's 20 results before they're sent to
the GUI thread and buffer them so the results are sent every .1
seconds. Nothing helps. Any advice would be great.
maybe you'ld better ask this question in the wxPython discussion group:

wx************@ lists.wxwidgets .org

cheers,
Stef
Jul 18 '07 #2
On Jul 18, 3:41 am, Benjamin <musiccomposit. ..@gmail.comwro te:
I'm writing a search engine in Python with wxPython as the GUI. I have
the actual searching preformed on a different thread from Gui thread.
It sends it's results through a Queue to the results ListCtrl which
adds a new item. This works fine or small searches, but when the
results number in the hundreds, the GUI is frozen for the duration of
the search. I suspect that so many search results are coming in that
the GUI thread is too busy updating lists to respond to events. I've
tried buffer the results so there's 20 results before they're sent to
the GUI thread and buffer them so the results are sent every .1
seconds. Nothing helps. Any advice would be great.
I do something similar - populating a bunch of comboboxes from data
takes a long time so I run the generator for the comboboxes in another
thread (and let the user use some textboxes in the mean time). A
rough edit toward what you might be doing:

import thread

class Processor(objec t):
def __init__(self):
self.lock = thread.allocate _lock()
self.alive = False
self.keepalive = False

def start(self):
if not self.alive:
self.alive = True
self.keepalive = True
thread.start_ne w_thread(self.P rocess, (None,))

def stop(self):
self.keepalive = False

def process(self, dummy=None):
self.alive = False
class SearchProcessor (Processor):
def __init__(self, entries, lookfor, report):
Processor.__ini t__(self)
self.entries = entries
self.lookfor = lookfor
self.report = report

def process(self, dummy=None):
for entry in self.entries:
if lookfor in entry:
self.report(ent ry)
if not self.keepalive:
break
self.report(Non e)
self.alive = False
results = []

def storeResult(res ult):
if result != None:
results.append( result)
else:
notifySomeMetho d(results)

sp = SearchProcessor (someListOfData , someTextToSearc hFor, storeResult)
sp.start()

when the search is done it will call notifySomeMetho d with the
results. Meanwhile you could, for example, bind sp.stop() to a cancel
button to stop the thread, or add a counter to the storeResult
function and set a status bar to the total found, or whatever else you
want to do.

Iain

Jul 19 '07 #3
Josiah Carlson <jo************ @sbcglobal.netw rote:
Sending results one at a time to the GUI is going to be slow for any
reasonably fast search engine (I've got a pure Python engine that does
50k results/second without breaking a sweat). Don't do that. Instead,
have your search thread create a list, which it fills with items for
some amount of time, *then* sends it off to the GUI thread (creating a
new list that it then fills, etc.). While you *could* use a Queue, it
is overkill for what you want to do (queues are really only useful when
there is actual contention for a resource and you want to block when a
resource is not available).
I'd dispute that. If you are communicating between threads use a
Queue and you will save yourself thread heartache. Queue has a non
blocking read interface Queue.get_nowai t().

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 19 '07 #4
Nick Craig-Wood wrote:
Josiah Carlson <jo************ @sbcglobal.netw rote:
> Sending results one at a time to the GUI is going to be slow for any
reasonably fast search engine (I've got a pure Python engine that does
50k results/second without breaking a sweat). Don't do that. Instead,
have your search thread create a list, which it fills with items for
some amount of time, *then* sends it off to the GUI thread (creating a
new list that it then fills, etc.). While you *could* use a Queue, it
is overkill for what you want to do (queues are really only useful when
there is actual contention for a resource and you want to block when a
resource is not available).

I'd dispute that. If you are communicating between threads use a
Queue and you will save yourself thread heartache. Queue has a non
blocking read interface Queue.get_nowai t().
If you have one producer and one consumer, and the consumer will be
notified when there is an item available, AND deques (in Python 2.4,
2.5, and presumably later) are threadsafe, then the *additional*
locking, blocking, etc., that a Queue provides isn't necessary.

Whether one wants to use a Queue for 'piece of mind', or for future
expansion possibilities is another discussion entirely, but his
application (as stated) will work with a deque for the worker thread ->
GUI thread communication path.

- Josiah
Jul 19 '07 #5
Josiah Carlson <jo************ @sbcglobal.netw rote:
Nick Craig-Wood wrote:
I'd dispute that. If you are communicating between threads use a
Queue and you will save yourself thread heartache. Queue has a non
blocking read interface Queue.get_nowai t().

If you have one producer and one consumer, and the consumer will be
notified when there is an item available, AND deques (in Python 2.4,
2.5, and presumably later) are threadsafe, then the *additional*
locking, blocking, etc., that a Queue provides isn't necessary.

Whether one wants to use a Queue for 'piece of mind', or for future
expansion possibilities is another discussion entirely, but his
application (as stated) will work with a deque for the worker thread ->
GUI thread communication path.
You are right deque's do say they are thread safe - I didn't know
that. From the docs :-

Deques support thread-safe, memory efficient appends and pops from
either side of the deque with approximately the same O(1) performance
in either direction.

I think I'd go for the peace of mind option, but YMMV of course ;-)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4490
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I happen to already own John Grayson's book about Tkinter programming, so that is rather handy if I decide to use Tkinter. I have done some things slightly more involved than "Hello World" in Tkinter - I have never used wxPython. So, basically the...
2
2762
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming methods of the two libraries: app.MainLoop, and reactor.run. Additionally, the fact that wxPython was to receive requests from the twisted framework as well as the end user seemed to be simply asking for trouble. My initial solution was, naturally,...
5
10670
by: fooooo | last post by:
This is a network app, written in wxPython and the socket module. This is what I want to happen: GUI app starts. User clicks a button to 'start' the work of the app. When start is pressed, a new thread is spawned (threading module) and this thread starts listening for data on a socket. When someone connects, a new thread is spawned, It needs to do I/O on that socket and open a GUI window so the user can communicate with the client...
6
2933
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I have a dialog box, wiht two buttoms "Start measurement" and "Stop". "Start" executes a function that do the measurement in the following way.
9
3252
by: perchef | last post by:
Hi, I have several files to download and a GUI to update. I know this is a frequently asked question but i can't find an appropriate solution. My Downloader extends threading.Thread and update a wx.Gauge in GUI during the process. for src in urls: downloader = Downloader( src, destination, GUI ) downloader.start()
2
3960
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an asynchronous pipe to the external tool via os.popen(). Then, it dumps the output from the external process into a text display. Although threads are often recommended for use with GUI apps, I am able to keep the GUI responsive with Tkinter's event loop,...
1
1745
by: Benjamin | last post by:
Hello! I am writing a search engine with wxPython as the GUI. As the search thread returns items, it adds them to a Queue which is picked up by the main GUI thread calling itself recursively with wx.CallAfter. These are then added to a ListCtrl. This works fine for small searches, but with larger and longer searchs the GUI is clogged and won't respond. I suspect (I may be wrong) that there are so many results being sent to the ListCtrl...
4
1928
by: Jimmy | last post by:
Hi, wxPython is cool and easy to use, But I ran into a problem recently when I try to write a GUI. The thing is I want to periodically update the content of StatixText object, so after create them, I pack them into a list...the problem comes when I later try to extract them from the list! I don't know why? my code is as following: import wx, socket import thread
12
2174
by: bullockbefriending bard | last post by:
I am a complete ignoramus and newbie when it comes to designing and coding networked clients (or servers for that matter). I have a copy of Goerzen (Foundations of Python Network Programming) and once pointed in the best direction should be able to follow my nose and get things sorted... but I am not quite sure which is the best path to take and would be grateful for advice from networking gurus. I am writing a program to display horse...
3
2399
by: mistersulu | last post by:
Hi all: I'm using a wx.ListView object with a multi-threaded wxPython app. The list is dynamically generated and accessed across two or more threads. In spite of the fact that I have checks to see if an item at a given index is in the list and the entire set of logic is contained within a try: except: block I periodically get pop-up windows stating "Could not retrieve information for list control item X" when I try to change or access...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9343
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.