or
how to use multiple threads in Pygtk (python)
Each "thing" going on independently in this case is called a Thread, short for thread of control.
how to use multiple threads in Pygtk (python)
You are probably familiar with multi-processing (also known as
multi-programming or multi-tasking): a single computer running several
programs (seemingly) simultaneously. This is done by clever and
frequent switching between the execution state of each program. For
example, the print spooler on your PC may be printing pages from one
document while you are editing another document in your word
processor.
Multi-threading is a finer-grained version of the same idea:
multiple things going on simultaneously within the same
program. For example, most web browsers allow you to continue to
browse while a file download is in progress.
Each "thing" going on independently in this case is called a Thread, short for thread of control.
Another reason for using threads is that the creation of a new
thread is much faster than the creation of a new process. This means
that when you have a relatively small amount of work that you would
like to handle separately,
import threading import time import gobject import gtk gobject.threads_init() from socket import * network = '192.168.1.' def is_up(addr): s = socket(AF_INET, SOCK_STREAM) s.settimeout(0.01) ## set a timeout of 0.01 sec if not s.connect_ex((addr,135)): # connect to the remote host on port 135 s.close() ## (port 135 is always open on Windows machines, AFAIK) return 1 else: s.close() class MyThread(threading.Thread): def __init__(self, label): super(MyThread, self).__init__() self.label = label self.quit = False def run(self): for i in xrange(1,256): addr = network + str(i) if is_up(addr): msg = '%s' %(getfqdn(addr)) iter = model.append() model.set(iter, 0, msg) w = gtk.Window() w.set_position(gtk.WIN_POS_CENTER) w.set_default_size(260, 300) scrolled_window = gtk.ScrolledWindow() scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scrolled_window.set_size_request(100,300) model = gtk.ListStore(gobject.TYPE_STRING) tree_view = gtk.TreeView(model) scrolled_window.add_with_viewport (tree_view) tree_view.show() cell = gtk.CellRendererToggle() cell.set_radio(False) column1 = gtk.TreeViewColumn("Select", cell) tree_view.append_column(column1) cell = gtk.CellRendererText() cell.set_property( 'editable', True ) column = gtk.TreeViewColumn("Computer Name\IP", cell, text=0) tree_view.append_column(column) w.add(scrolled_window) w.show_all() w.connect("destroy", lambda _: gtk.main_quit()) #This is thread t = MyThread(model) t.start() gtk.main() t.quit = TruePython supports some very simple optional operations for threading. Suppport for these operations must be requested when Python is built for a particular installation; it is available for most platforms except for Macintosh. In particular, threading is supported for Windows NT and 95, for Unix systems with a POSIX thread implementation (this includes Linux), and for native threads on Sun Solaris and SGI IRIX.
Thanks
No comments:
Post a Comment