简体   繁体   中英

Python Thread communication not working

I have two threads, Reader and Writer.

The Writer gets data from the network and sends it then over a socket to some executable. When this is done the writer should block up to 70 seconds which I specify with a Event.wait(askrate).

This should give the executable enough time to compute the result and then submit the output. If the computation is finished I used Event.set() to release the lock on the Writer thread so that it can read the next data that is forwared to the executeable and so on.

The problem that I have is, that the Writer thread still keeps reading data while the Reader thread is waiting for the result coming through the serial interface.

Anyone an idea why this blocking meachnism is not proberly working between these two threads?

askrate = 70

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/demo_socket")

class Reader(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True

    def run(self):
        while True:            
            nonce = s.recv(4)            
            if len(nonce) == 4:
                submitter = Submitter(writer.block, nonce)
                #submit result and release thread lock in Writer class 
                golden.set()

class Writer(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True

    def run(self):
        while True:
          work = bc.getwork()                
          self.block = work['data']
          self.midstate = work['midstate']

          payload = self.midstate.decode('hex') + self.block.decode('hex')
          s.send(payload)
          result = golden.wait(askrate)

          if result:
             golden.clear()


golden = Event()

reader = Reader()
writer = Writer()

reader.start()
writer.start() 

I'm pretty sure that it's not how you are supposed to use AF_UNIX sockets. You are supposed to open the pseudo-file twice (from the same of different processes); then writes to one side appear as reads on the other side, and vice-versa. In your code, you open the pseudo-file only once. Any write is probably blocking, waiting for another process to open the pseudo-file a second time.

In your case, you should use socket.socketpair(), which returns you two sockets at once, playing the role of the two ends. Use one end in each thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM