Please see the section on thread interruption in the Jython book chapter on concurrency:

This section discusses how to use Python's standard threading library with the enhancements available under Java, such as interruption. Also 2.5.2 now provides support of java.lang.Thread methods without monkeypatching.

- Jim

On Thu, Aug 12, 2010 at 9:07 AM, Brandon Pedersen <> wrote:
On Wed, Aug 11, 2010 at 3:30 PM, Raghuram Devarakonda
<> wrote:
> I tried this example with trunk and I do see the print statement as a
> result of the interruption.  I don't have 2.5.1 handy in order to
> verify whether this is something that got fixed in the trunk. Will you
> be able to test with Jython from trunk?

I will have to try this again later. I found a different method to do
what I want...and cleaner, but I will see if I can reproduce with
trunk. It may have to do with handling sockets which is what I am
doing. Here is a more complete example of my previous work if you want
to try again:

class Listener(Thread): # <-- this is a java.lang.Thread, not
threading.Thread since threading.Thread does not have interrupt()

   def __init__(self, port=0):
       Thread.__init__(self, "Listener")
       self.port = port
       self._log = StringIO()
       self.captured_output = None
       self._client_socket = None
       self._server = None

   def run(self):
       self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       self._server.bind(("", self.port))
       self.port = self._server.getsockname()[1]

       log.debug("Listener started on port %d" % self.port)

       self._client_socket, address = self._server.accept()

       log.debug("Listener got a connection from " + str(address))

           while True:
               data = self._client_socket.recv(1024)
           print "in finally block"
               if self._client_socket:
           except: pass
               if self._server:
           except: pass
           self.captured_output = self._log.getvalue()
           log.trace("%s output:\n%s" % (self.getName(), self.captured_output))

if __name__ == "__main__":
  tmp = Listener()
  # start something that connects to the port so socket.recv() will block
  # the print statement in the finally block (or anything in an
except block) is never seen



This email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
Jython-users mailing list