Differences between revisions 16 and 17
Revision 16 as of 2003-06-07 11:27:03
Size: 3818
Editor: cacher1-ext
Comment:
Revision 17 as of 2004-11-26 13:43:51
Size: 5208
Editor: host89-161
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Stopping Threads = = Stopping Threads  Blocco dei processi= 
Line 4: Line 4:

Inizio questa pagina facendo una domanda. Come si può fermare un processo dall'interno di un altro?
Line 35: Line 37:
== Suggestions == Il problema è che {{{EventChannel.waitEvent()}}} è una operazione di blocco. Così se l'evento non si verifica, allora il nostro lavoro non si fermerà mai. ''({{{EventChannel}}} e {{{EventHandler}}} sono classi che io ho inventato per questo esempio)''


== Suggestions Suggerimenti==
Line 38: Line 43:
   * Utilizzare il metodo {{{shutdown()}}} spinge alcuni innocui eventi nelll'evento channel
  
Line 46: Line 53:

 * oppure usa una coda di dati passati. Manipolare errori con il manipolatore ( e.g. stampa poi alla console),mantiene vivo il processo
Line 104: Line 113:
= Call to C function blocks all threads = = Call to C function blocks all threads  Chiamata ad una Funzione "C" che blocca tutti i processi=
Line 109: Line 118:

Ho un modulo in "c" che pone delle interrogazioni al D.B. Queste interrogazioni pongono ad off il server SQLper essere processato.Posso usare delle mie funzioni di interrogazione dentro i processi per avere un lavoro come questo {{{time.sleep()}}},blocco il corrente processo finchè lui termina ma permette agli altri processi di continuare le operazioni. Non ho visto questi inidirizzamenti in qualsiasi libro che possiedo.
Line 111: Line 122:
" Ciò può essere attuato con il codice python. L'autore del D.B.
= Resources Risorse =
Line 112: Line 125:
= Resources =

* [http://starship.python.net/crew/aahz/OSCON2001/index.html Aahz OSCON 2001 presentation]
 * [http://starship.python.net/crew/aahz/OSCON2001/index.html Aahz OSCON 2001 presentatione]

= Stopping Threads Blocco dei processi=

I'd like to start this page off with a question. How do you kill one thread from within another? Here's some code that shows the problem:

Inizio questa pagina facendo una domanda. Come si può fermare un processo dall'interno di un altro?

   1 import threading
   2 import time
   3 
   4 class Worker(threading.Thread):
   5   def __init__(self, eventChannel, eventHandler):
   6     self.eventChannel = eventChannel
   7     self.eventHandler = eventHandler
   8     self.stopFlag = 0
   9 
  10   def shutdown(self):
  11     self.stopFlag = 1
  12 
  13   def run(self):
  14     self.stopFlag = 0
  15     while not self.stopFlag:
  16       event = self.eventChannel.waitEvent() # blocking call
  17       self.eventHandler.dispatchEvent(event)
  18 
  19 
  20 eventChannel = EventChannel()
  21 eventHandler = EventHandler()
  22 worker = Worker(eventChannel, eventHandler)
  23 worker.start()
  24 time.sleep(20)
  25 worker.shutdown()

The problem here is that EventChannel.waitEvent() is a blocking operation. So if no event ever arrives, then our worker thread never stops. (EventChannel and EventHandler are classes I've invented for this example)

Il problema è che EventChannel.waitEvent() è una operazione di blocco. Così se l'evento non si verifica, allora il nostro lavoro non si fermerà mai. (EventChannel e EventHandler sono classi che io ho inventato per questo esempio)

== Suggestions Suggerimenti==

  • Make the shutdown() method put some harmless event on the event channel:

  • Utilizzare il metodo shutdown() spinge alcuni innocui eventi nelll'evento channel

  def shutdown(self):
    self.stopFlag = 1
    self.eventChannel.push_event(NullEvent())
  • Or use a Queue to pass data. Handle errors in the handler (e.g. print them to the console), keep the thread alive.
  • oppure usa una coda di dati passati. Manipolare errori con il manipolatore ( e.g. stampa poi alla console),mantiene vivo il processo

   1 import Queue, threading, traceback
   2 
   3 class StopMarker:
   4     """This is used as an individual stopper item."""
   5     
   6 class Worker(threading.Thread):
   7     """Get items from a queue and call the handler with the item.
   8     Errors in the handler are printed to stderr, the thread
   9     continues to run.
  10     An individual stop marker is used, so you can pass everyting
  11     as item (including None).
  12     """
  13     
  14     def __init__(self, handler):
  15         """Initialize Thread object and worker."""
  16         threading.Thread.__init__(self)
  17         self.running = 1
  18         self.handler = handler
  19         self.queue   = Queue.Queue()
  20         self.stopper = StopMarker()
  21     
  22     def run(self):
  23         """Worker loop. Errors in the handler are printed to
  24         stderr and the thread continues with the next item."""
  25         while self.running:
  26             try:
  27                 item = self.queue.get()
  28                 if item is self.stopper:
  29                     break
  30                 else:
  31                     self.handler(item)
  32             except:
  33                 traceback.print_exc()
  34     
  35     def stop(self):
  36         """Stop the worker thread and wait until it terminates.
  37         (Waits until last item is processed)"""
  38         self.queue.put(self.stopper)    #stopper item, then...
  39         self.join()                     #wait until the thread has shutdown
  40 
  41     def put(self, item):
  42         """Put an item in the queue to be processed by the handler."""
  43         self.queue.put(item)
  44 
  45 if __name__ == '__main__':
  46     def printer(item):
  47         print "printer(%r)" % item
  48     
  49     w = Worker(printer)
  50     w.start()
  51     for i in range(10):
  52         w.put(i)
  53     w.stop()

= Call to C function blocks all threads Chiamata ad una Funzione "C" che blocca tutti i processi=

I have a C module that does database queries. Those queries go off to an SQL server to be processed. I would like to use my query function within threads and to have it work like time.sleep(), that is, block the current thread until it finishes but allow other threads to continue operation. I haven't seen this issue addressed in any of the books I have.

Ho un modulo in "c" che pone delle interrogazioni al D.B. Queste interrogazioni pongono ad off il server SQLper essere processato.Posso usare delle mie funzioni di interrogazione dentro i processi per avere un lavoro come questo time.sleep(),blocco il corrente processo finchè lui termina ma permette agli altri processi di continuare le operazioni. Non ho visto questi inidirizzamenti in qualsiasi libro che possiedo.

  • This can't be handled in Python code. The author of the database module should use Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS around blocking calls to permit other threads to run.

" Ciò può essere attuato con il codice python. L'autore del D.B.

Resources Risorse

ThreadProgramming (last edited 2008-11-15 13:59:41 by localhost)

Unable to edit the page? See the FrontPage for instructions.