|
1 | 1 | from HTMLParser import HTMLParser |
2 | 2 | import inspect |
| 3 | +import multiprocessing |
3 | 4 | import re |
| 5 | +import Queue |
4 | 6 | from urllib import quote, quote_plus |
5 | 7 | from urlparse import urlparse |
6 | | -import multiprocessing |
7 | | -import Queue |
8 | 8 | from debug import logger |
| 9 | +from shared import parserInputQueue, parserOutputQueue, parserProcess, parserLock |
9 | 10 |
|
10 | | -def regexpSubprocess(queue): |
11 | | - try: |
12 | | - result = queue.get() |
13 | | - result = SafeHTMLParser.uriregex1.sub( |
14 | | - r'<a href="\1">\1</a>', |
15 | | - result) |
16 | | - result = SafeHTMLParser.uriregex2.sub(r'<a href="\1&', result) |
17 | | - result = SafeHTMLParser.emailregex.sub(r'<a href="mailto:\1">\1</a>', result) |
18 | | - except: |
19 | | - pass |
20 | | - else: |
21 | | - queue.put(result) |
| 11 | +def regexpSubprocess(parserInputQueue, parserOutputQueue): |
| 12 | + for data in iter(parserInputQueue.get, None): |
| 13 | + if data is None: |
| 14 | + break; |
| 15 | + try: |
| 16 | + result = SafeHTMLParser.uriregex1.sub( |
| 17 | + r'<a href="\1">\1</a>', |
| 18 | + data) |
| 19 | + result = SafeHTMLParser.uriregex2.sub(r'<a href="\1&', result) |
| 20 | + result = SafeHTMLParser.emailregex.sub(r'<a href="mailto:\1">\1</a>', result) |
| 21 | + parserOutputQueue.put(result) |
| 22 | + except SystemExit: |
| 23 | + break; |
| 24 | + except: |
| 25 | + break; |
22 | 26 |
|
23 | 27 | class SafeHTMLParser(HTMLParser): |
24 | 28 | # from html5lib.sanitiser |
@@ -106,22 +110,28 @@ def handle_entityref(self, name): |
106 | 110 | self.sanitised += "&" + name + ";" |
107 | 111 |
|
108 | 112 | def feed(self, data): |
| 113 | + global parserLock, parserProcess, parserInputQueue, parserOutputQueue |
109 | 114 | HTMLParser.feed(self, data) |
110 | 115 | tmp = SafeHTMLParser.multi_replace(data) |
111 | 116 | tmp = unicode(tmp, 'utf-8', 'replace') |
112 | | - |
113 | | - queue = multiprocessing.Queue() |
114 | | - parser = multiprocessing.Process(target=regexpSubprocess, name="RegExParser", args=(queue,)) |
115 | | - parser.start() |
116 | | - queue.put(tmp) |
117 | | - parser.join(1) |
118 | | - if parser.is_alive(): |
119 | | - parser.terminate() |
120 | | - parser.join() |
| 117 | + |
| 118 | + parserLock.acquire() |
| 119 | + if parserProcess is None: |
| 120 | + parserProcess = multiprocessing.Process(target=regexpSubprocess, name="RegExParser", args=(parserInputQueue, parserOutputQueue)) |
| 121 | + parserProcess.start() |
| 122 | + parserLock.release() |
| 123 | + parserInputQueue.put(tmp) |
121 | 124 | try: |
122 | | - tmp = queue.get(False) |
123 | | - except (Queue.Empty, StopIteration) as e: |
| 125 | + tmp = parserOutputQueue.get(True, 1) |
| 126 | + except Queue.Empty: |
124 | 127 | logger.error("Regular expression parsing timed out, not displaying links") |
| 128 | + parserLock.acquire() |
| 129 | + parserProcess.terminate() |
| 130 | + parserProcess = multiprocessing.Process(target=regexpSubprocess, name="RegExParser", args=(parserInputQueue, parserOutputQueue)) |
| 131 | + parserProcess.start() |
| 132 | + parserLock.release() |
| 133 | + else: |
| 134 | + pass |
125 | 135 | self.raw += tmp |
126 | 136 |
|
127 | 137 | def is_html(self, text = None, allow_picture = False): |
|
0 commit comments