Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. About threads and callbacks
  2. import threading
  3.  
  4. def cb_func(data):
  5.     """The callback function"""
  6.     print data
  7.  
  8. def th_func(callback):
  9.     """The threaded function"""
  10.     # do some work here
  11.     callback('somedata')
  12.  
  13. thr = threading.Thread(target=th_func, args=(cb_func,)).start()
  14.        
  15. import threading
  16.  
  17. def cb_func():
  18.     "The callback function."
  19.     print 'Callback, in thread %s' % threading.current_thread().name
  20.  
  21. def th_func(callback):
  22.     "The threaded function."
  23.     # ...
  24.     callback()
  25.  
  26. thr = threading.Thread(target=th_func, args=(cb_func,)).start()
  27.        
  28. Callback, in thread Thread-1`