Communicating between threads

Petr Kubanek petr at kubanek.net
Fri Sep 28 15:49:00 EDT 2001


Hi,

let's make it simple:

----------------- code 
#!/usr/bin/python
import time
import random

class test:
  def __init__(self):
    print "Starting threads"
    self.seed = 0
    thread.start_new_thread(self.increm,())
    thread.start_new_thread(self.reader,())


  def increm(self):
    while 1:
      self.seed = self.seed+1
      print "Incremented - new value:"+str(self.seed)
      time.sleep(random.random()*10)

  def reader(self):
    while 1:
      print "Seed value: "+str(self.seed)
      time.sleep(5)

a = test()
while 1:
  print "Running"
  time.sleep(50)
-----------------code

You don't need any signals etc., simple make class and change value in it.

You are right with signal, it's performing randomly when more threads are 
running. If you realy need sending signal, don't use threads, use fork.

Petr Kubanek

fladak at rri.on.ca wrote:

> Does anyone know how to communicate between threads?  It might help if I
> explained my application.
> 
> I have two instances of an application, written in python, running.  I
> refer to
> the application as a "viewer".  Both instances have two threads.  The main
> thread (I'll refer to it as thread A) handles the GUI events and the other
> thread (I'll refer to it as thread B) communicates to the other viewer
> using
> OmniOrb, an implementation of CORBA.  I'm trying to develop the code so
> that if you rotate the image in one viewer, the image in the other viewer
> rotates.
> 
> When I rotate the image in viewer 1, thread A in viewer 1 communicates to
> thread
> B in viewer 2 and requests a rotation.  Since thread A handles all the GUI
> events, I need some way to communicate this from thread B in viewer 2 to
> thread A in viewer 2.
> 
> I can't use IPC or uni-directional named pipes here because thread A is
> already busy listening for events, so it can't listen on a pipe or a
> socket.
> 
> I also tried using signals but experienced some problems in Linux and
> Windows. Under Linux, thread A which is the main thread registers the
> signal handler and if you send a signal from a thread other than the main
> thread (in this case
> thread B) the signal handler is not called.  You have to send the signal
> from
> the main thread to have the signal handler called.  Thus I can't use this
> as
> means to communicate between threads.  It seems that signals are meant as
> a way
> to communicate between processes not threads.  I wrote a test program
> where I
> sent a signal between threads and it didn't work.  Under Windows, the
> kill() function which is used to send the signal just doesn't exist in
> python and many of the signals like SIGUSR1 don't exist.
> 
> Does anyone have any suggestions?  Thanks.
> 
> -Faruq
> 
> ---------------------------------------------
> This message was sent using Endymion MailMan.
> http://www.endymion.com/products/mailman/




More information about the Python-list mailing list