Python threading + socket IO lockup problem in Cygwin
I am having a weird concurrency issue with Python on Cygwin. I am running two threads:
- A thread which sends UDP packets at regular intervals
- A main thread which waits for KeyBoardInterrupt and when it receives one, it tells the UDP thread to stop sending via a boolean flag
This works just fine under Linux (python 2.6) and Windows (Python 2.7) but has issues with the Cygwin's Python (2.6).
In Cygwin the socket sending thread waits until the main thread gets any input (raw_input) from user before it sends it's UDP packet. This happens each time. Here is the code:
import sys
import socket
import threading
from time import sleep
class UdpPacketSender(threading.Thread):
def __init__(self, address, port):
threading.Thread.__init__ (self)
self.address = (address, int(port))
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.send = True
def run(self):
while self.send:
print "Sending packet..."
self.socket.sendto("Hello", self.address)
print "sent packet"
sleep(1)
self.socket.close()
def stop_sending(self):
self.send = False
sender = UdpPacketSender("127.0.0.1", 7000)
sender.start()
try:
while True:
raw_input()
except KeyboardInterrupt:
print "Got keyboard interrupt, stopping"
sender.stop_se开发者_JS百科nding()
So under Cygwin I never get the printout "sent packet" until I e.g. press enter on the console window. After I press it, I get one "sent packet" but no more until I press some key again. In native windows Python and Linux I get "sent packet" every second.
I'd like to use the Cygwin version in windows for development, but I'm starting to wonder whether it's not really working properly. I quess this has something to do with GIL? I also tried a version where the send-flag is synchronized with threading.RLock, but it doesn't change the behavior.
If you haven't updated your cygwin (cygwin 1.5.5-1+), there are some pthreads bugs I believe. You might want to try running the code with the latest snapshot to rule that out.
精彩评论