import Queue
import wx, wx.stc
from editor_fonts import init_stc_style

class ThreadOutputCtrl(wx.stc.StyledTextCtrl):
    def __init__(self, parent, style=wx.TE_READONLY):
        wx.stc.StyledTextCtrl.__init__(self, parent)
        init_stc_style(self)
        self.SetIndent(4)
        self.SetTabWidth(8)
        self.SetUseTabs(False)

        self.queue = Queue.Queue(1024)
        self.timer = wx.Timer(self)

        self.Bind(wx.EVT_TIMER, self.__OnTimer, self.timer)

    def __OnTimer(self, evt):
        self.flush()

    def flush(self):
        lines = "".join(self.queue.get_nowait() for _ in xrange(self.queue.qsize()))
        if lines:
            self.AppendText(lines)

    def start(self, interval=100):
        self.timer.Start(interval)

    def stop(self):
        self.timer.Stop()
        wx.CallAfter(self.flush)

    def write(self, s):
        self.queue.put(s)

    def IsEmpty(self):
        return self.GetTextLength() == 0

import threading
import wx, wx.stc
from editor_fonts import init_stc_style

class ThreadOutputCtrl(wx.stc.StyledTextCtrl):
    def __init__(self, parent, style=wx.TE_READONLY):
        wx.stc.StyledTextCtrl.__init__(self, parent)
        init_stc_style(self)
        self.SetIndent(4)
        self.SetTabWidth(8)
        self.SetUseTabs(False)

        self.__lock = threading.Lock()
        self.__queue = []
        self.__timer = wx.Timer(self)

        self.Bind(wx.EVT_TIMER, self.__OnTimer, self.__timer)

    def __OnTimer(self, evt):
        self.flush()

    def flush(self):
        with self.__lock:
            queue, self.__queue = self.__queue, []
        lines = "".join(queue)
        if lines:
            self.AppendText(lines)

    def start(self, interval=100):
        self.__timer.Start(interval)

    def stop(self):
        self.__timer.Stop()
        wx.CallAfter(self.flush)

    def write(self, s):
        with self.__lock:
            self.__queue.append(s)

    def IsEmpty(self):
        return self.GetTextLength() == 0

Use a lock-protected list instead of a Queue.
Use a lock-protected list instead of a Queue.

- import Queue
+ import threading
  import wx, wx.stc
  from editor_fonts import init_stc_style
  
  class ThreadOutputCtrl(wx.stc.StyledTextCtrl):
      def __init__(self, parent, style=wx.TE_READONLY):
          wx.stc.StyledTextCtrl.__init__(self, parent)
          init_stc_style(self)
          self.SetIndent(4)
          self.SetTabWidth(8)
          self.SetUseTabs(False)
  
-         self.queue = Queue.Queue(1024)
+         self.__lock = threading.Lock()
+         self.__queue = []
-         self.timer = wx.Timer(self)
+         self.__timer = wx.Timer(self)
  
-         self.Bind(wx.EVT_TIMER, self.__OnTimer, self.timer)
+         self.Bind(wx.EVT_TIMER, self.__OnTimer, self.__timer)
  
      def __OnTimer(self, evt):
          self.flush()
  
      def flush(self):
-         lines = "".join(self.queue.get_nowait() for _ in xrange(self.queue.qsize()))
+         with self.__lock:
+             queue, self.__queue = self.__queue, []
+         lines = "".join(queue)
          if lines:
              self.AppendText(lines)
  
      def start(self, interval=100):
-         self.timer.Start(interval)
+         self.__timer.Start(interval)
  
      def stop(self):
-         self.timer.Stop()
+         self.__timer.Stop()
          wx.CallAfter(self.flush)
  
      def write(self, s):
+         with self.__lock:
-         self.queue.put(s)
+             self.__queue.append(s)
  
      def IsEmpty(self):
          return self.GetTextLength() == 0
  
Use a lock-protected list instead of a Queue.
## Code Before:
import Queue
import wx, wx.stc
from editor_fonts import init_stc_style

class ThreadOutputCtrl(wx.stc.StyledTextCtrl):
    def __init__(self, parent, style=wx.TE_READONLY):
        wx.stc.StyledTextCtrl.__init__(self, parent)
        init_stc_style(self)
        self.SetIndent(4)
        self.SetTabWidth(8)
        self.SetUseTabs(False)

        self.queue = Queue.Queue(1024)
        self.timer = wx.Timer(self)

        self.Bind(wx.EVT_TIMER, self.__OnTimer, self.timer)

    def __OnTimer(self, evt):
        self.flush()

    def flush(self):
        lines = "".join(self.queue.get_nowait() for _ in xrange(self.queue.qsize()))
        if lines:
            self.AppendText(lines)

    def start(self, interval=100):
        self.timer.Start(interval)

    def stop(self):
        self.timer.Stop()
        wx.CallAfter(self.flush)

    def write(self, s):
        self.queue.put(s)

    def IsEmpty(self):
        return self.GetTextLength() == 0

## Instruction:
Use a lock-protected list instead of a Queue.
## Code After:
import threading
import wx, wx.stc
from editor_fonts import init_stc_style

class ThreadOutputCtrl(wx.stc.StyledTextCtrl):
    def __init__(self, parent, style=wx.TE_READONLY):
        wx.stc.StyledTextCtrl.__init__(self, parent)
        init_stc_style(self)
        self.SetIndent(4)
        self.SetTabWidth(8)
        self.SetUseTabs(False)

        self.__lock = threading.Lock()
        self.__queue = []
        self.__timer = wx.Timer(self)

        self.Bind(wx.EVT_TIMER, self.__OnTimer, self.__timer)

    def __OnTimer(self, evt):
        self.flush()

    def flush(self):
        with self.__lock:
            queue, self.__queue = self.__queue, []
        lines = "".join(queue)
        if lines:
            self.AppendText(lines)

    def start(self, interval=100):
        self.__timer.Start(interval)

    def stop(self):
        self.__timer.Stop()
        wx.CallAfter(self.flush)

    def write(self, s):
        with self.__lock:
            self.__queue.append(s)

    def IsEmpty(self):
        return self.GetTextLength() == 0
