Skip to content

Commit eeb60b0

Browse files
author
Stephen Hynes
committed
Merge pull request LogentriesCommunity#38 from graingert/avoid-using-lock-in-LE
Avoid using lock in emit
2 parents bed7905 + af9b1e5 commit eeb60b0

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

logentries/utils.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import time
1313
import sys
1414

15-
lock = threading.Lock()
16-
1715
import certifi
1816

1917
# Size of the internal event queue
@@ -168,10 +166,6 @@ def __init__(self, token, force_tls=False, verbose=True, format=None):
168166
else:
169167
self._thread = SocketAppender(verbose=verbose)
170168

171-
@property
172-
def _started(self):
173-
return self._thread.is_alive()
174-
175169
def flush(self):
176170
# wait for all queued logs to be send
177171
now = time.time()
@@ -183,19 +177,19 @@ def flush(self):
183177
def emit(self, record):
184178
# Reset stdout. See: http://docs.python.org/2/library/sys.html#sys.__stdout__
185179
sys.stdout = sys.__stdout__
186-
lock.acquire()
187-
try:
188-
if not self._started and self.good_config:
180+
if self.good_config and not self._thread.is_alive():
181+
try:
182+
self._thread.start()
189183
if self.verbose:
190184
dbg("Starting Logentries Asynchronous Socket Appender")
191-
self._thread.start()
185+
except RuntimeError: # It's already started.
186+
if not self._thead.is_alive():
187+
raise
192188

193-
msg = self.format(record).rstrip('\n')
194-
msg = self.token + msg
189+
msg = self.format(record).rstrip('\n')
190+
msg = self.token + msg
195191

196-
self._thread._queue.put(msg)
197-
finally:
198-
lock.release()
192+
self._thread._queue.put(msg)
199193

200194
def close(self):
201195
logging.Handler.close(self)

0 commit comments

Comments
 (0)