From 2c7e40bd77515065dc0bcdba3b724b9d071ab31c Mon Sep 17 00:00:00 2001
From: RaNaN <Mast3rRaNaN@hotmail.de>
Date: Wed, 29 Jul 2009 12:57:06 +0200
Subject: captcha reading timeout, to avoid hang up

---
 captcha/captcha.py | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

(limited to 'captcha')

diff --git a/captcha/captcha.py b/captcha/captcha.py
index 8f0b828d8..6d4cd86e8 100644
--- a/captcha/captcha.py
+++ b/captcha/captcha.py
@@ -1,3 +1,4 @@
+import thread
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 #
@@ -22,6 +23,29 @@ import tempfile
 
 import Image
 import ImageOps
+import threading
+
+class RunThread(threading.Thread):
+    def __init__(self):
+        threading.Thread.__init__(self)
+
+    def e(self, command, inputdata=None):
+        """execute command """
+        self.command = command
+        self.inputdata = inputdata
+        self.result = ""
+        self.start()
+        self.join(10)
+        return self.result
+
+    def run(self):
+        """Run a command and return standard output"""
+        pipe = subprocess.PIPE
+        popen = subprocess.Popen(self.command, stdout=pipe, stderr=pipe)
+        outputdata, errdata = popen.communicate(self.inputdata)
+        assert (popen.returncode == 0), \
+            "Error running: %s\n\n%s" % (self.command, errdata)
+        self.result = outputdata
 
 class OCR(object):
     def __init__(self):
@@ -41,12 +65,18 @@ class OCR(object):
 
     def run(self, command, inputdata=None):
         """Run a command and return standard output"""
-        pipe = subprocess.PIPE
-        popen = subprocess.Popen(command, stdout=pipe, stderr=pipe)
-        outputdata, errdata = popen.communicate(inputdata)
-        assert (popen.returncode == 0), \
-            "Error running: %s\n\n%s" % (command, errdata)
-        return outputdata
+#        OLD METHOD
+#        pipe = subprocess.PIPE
+#        popen = subprocess.Popen(command, stdout=pipe, stderr=pipe)
+#        outputdata, errdata = popen.communicate(inputdata)
+#        assert (popen.returncode == 0), \
+#            "Error running: %s\n\n%s" % (command, errdata)
+#        return outputdata
+
+        thread = RunThread()
+        result = thread.e(command, inputdata)
+        print result
+        return result
 
     def run_gocr(self):
         tmp = tempfile.NamedTemporaryFile(suffix=".jpg")
-- 
cgit v1.2.3