Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/08/2010, 09:03
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 8 meses
Puntos: 1360
Respuesta: Thead wxpython

Aquí hay varios ejemplos

Código Python:
Ver original
  1. import time
  2. from threading import *
  3. import wx
  4.  
  5. # Button definitions
  6. ID_START = wx.NewId()
  7. ID_STOP = wx.NewId()
  8.  
  9. # Define notification event for thread completion
  10. EVT_RESULT_ID = wx.NewId()
  11.  
  12. def EVT_RESULT(win, func):
  13.     """Define Result Event."""
  14.     win.Connect(-1, -1, EVT_RESULT_ID, func)
  15.  
  16. class ResultEvent(wx.PyEvent):
  17.     """Simple event to carry arbitrary result data."""
  18.     def __init__(self, data):
  19.         """Init Result Event."""
  20.         wx.PyEvent.__init__(self)
  21.         self.SetEventType(EVT_RESULT_ID)
  22.         self.data = data
  23.  
  24. # Thread class that executes processing
  25. class WorkerThread(Thread):
  26.     """Worker Thread Class."""
  27.     def __init__(self, notify_window):
  28.         """Init Worker Thread Class."""
  29.         Thread.__init__(self)
  30.         self._notify_window = notify_window
  31.         self._want_abort = 0
  32.         # This starts the thread running on creation, but you could
  33.         # also make the GUI thread responsible for calling this
  34.         self.start()
  35.  
  36.     def run(self):
  37.         """Run Worker Thread."""
  38.         # This is the code executing in the new thread. Simulation of
  39.         # a long process (well, 10s here) as a simple loop - you will
  40.         # need to structure your processing so that you periodically
  41.         # peek at the abort variable
  42.         for i in range(10):
  43.             time.sleep(1)
  44.             if self._want_abort:
  45.                 # Use a result of None to acknowledge the abort (of
  46.                 # course you can use whatever you'd like or even
  47.                 # a separate event type)
  48.                 wx.PostEvent(self._notify_window, ResultEvent(None))
  49.                 return
  50.         # Here's where the result would be returned (this is an
  51.         # example fixed result of the number 10, but it could be
  52.         # any Python object)
  53.         wx.PostEvent(self._notify_window, ResultEvent(10))
  54.  
  55.     def abort(self):
  56.         """abort worker thread."""
  57.         # Method for use by main thread to signal an abort
  58.         self._want_abort = 1
  59.  
  60. # GUI Frame class that spins off the worker thread
  61. class MainFrame(wx.Frame):
  62.     """Class MainFrame."""
  63.     def __init__(self, parent, id):
  64.         """Create the MainFrame."""
  65.         wx.Frame.__init__(self, parent, id, 'Thread Test')
  66.  
  67.         # Dumb sample frame with two buttons
  68.         wx.Button(self, ID_START, 'Start', pos=(0,0))
  69.         wx.Button(self, ID_STOP, 'Stop', pos=(0,50))
  70.         self.status = wx.StaticText(self, -1, '', pos=(0,100))
  71.  
  72.         self.Bind(wx.EVT_BUTTON, self.OnStart, id=ID_START)
  73.         self.Bind(wx.EVT_BUTTON, self.OnStop, id=ID_STOP)
  74.  
  75.         # Set up event handler for any worker thread results
  76.         EVT_RESULT(self,self.OnResult)
  77.  
  78.         # And indicate we don't have a worker thread yet
  79.         self.worker = None
  80.  
  81.     def OnStart(self, event):
  82.         """Start Computation."""
  83.         # Trigger the worker thread unless it's already busy
  84.         if not self.worker:
  85.             self.status.SetLabel('Starting computation')
  86.             self.worker = WorkerThread(self)
  87.  
  88.     def OnStop(self, event):
  89.         """Stop Computation."""
  90.         # Flag the worker thread to stop if running
  91.         if self.worker:
  92.             self.status.SetLabel('Trying to abort computation')
  93.             self.worker.abort()
  94.  
  95.     def OnResult(self, event):
  96.         """Show Result status."""
  97.         if event.data is None:
  98.             # Thread aborted (using our convention of None return)
  99.             self.status.SetLabel('Computation aborted')
  100.         else:
  101.             # Process results here
  102.             self.status.SetLabel('Computation Result: %s' % event.data)
  103.         # In either event, the worker is done
  104.         self.worker = None
  105.  
  106. class MainApp(wx.App):
  107.     """Class Main App."""
  108.     def OnInit(self):
  109.         """Init Main App."""
  110.         self.frame = MainFrame(None, -1)
  111.         self.frame.Show(True)
  112.         self.SetTopWindow(self.frame)
  113.         return True
  114.  
  115. if __name__ == '__main__':
  116.     app = MainApp(0)
  117.     app.MainLoop()

En cuanto al numero de threads depende de cuantos núcleos tenga tu procesador. Ademas de otros factores como si haces alguna sincronización o si esperas alguna entrada o salida.

Aquí una discusión completa sobre el numero optimo de threads

En este caso en particular si solo son 4 fotos le pondría un thread por foto.