Skip to content

Commit

Permalink
Refactor the usage of ThreadSafeBuffer
Browse files Browse the repository at this point in the history
This should fix #73
  • Loading branch information
romanvm committed Nov 16, 2024
1 parent 28ac28c commit a27dbdf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
55 changes: 55 additions & 0 deletions web_pdb/buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Author: Roman Miroshnychenko aka Roman V.M.
# E-mail: roman1972@gmail.com
#
# Copyright (c) 2016 Roman Miroshnychenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from threading import RLock

__all__ = ['ThreadSafeBuffer']


class ThreadSafeBuffer:
"""
A buffer for data exchange between threads
"""
def __init__(self, contents=None):
self._lock = RLock()
self._contents = contents
self._is_dirty = contents is not None

@property
def is_dirty(self):
"""Indicates whether a buffer contains unread data"""
with self._lock:
return self._is_dirty

@property
def contents(self):
"""Get or set buffer contents"""
with self._lock:
self._is_dirty = False
return self._contents

@contents.setter
def contents(self, value):
with self._lock:
self._contents = value
self._is_dirty = True
36 changes: 4 additions & 32 deletions web_pdb/web_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,16 @@
import time
import weakref
from socket import gethostname
from threading import Thread, Event, RLock
from threading import Thread, Event

from asyncore_wsgi import make_server, AsyncWebSocketHandler

from .buffer import ThreadSafeBuffer
from .wsgi_app import app

__all__ = ['WebConsole']


class ThreadSafeBuffer:
"""
A buffer for data exchange between threads
"""
def __init__(self, contents=None):
self._lock = RLock()
self._contents = contents
self._is_dirty = contents is not None

@property
def is_dirty(self):
"""Indicates whether a buffer contains unread data"""
with self._lock:
return self._is_dirty

@property
def contents(self):
"""Get or set buffer contents"""
with self._lock:
self._is_dirty = False
return self._contents

@contents.setter
def contents(self, value):
with self._lock:
self._contents = value
self._is_dirty = True


class WebConsoleSocket(AsyncWebSocketHandler):
"""
WebConsoleSocket receives PDB commands from the front-end and
Expand Down Expand Up @@ -98,7 +70,7 @@ class WebConsole:
def __init__(self, host, port, debugger):
self._debugger = weakref.proxy(debugger)
self._console_history = ThreadSafeBuffer('')
self._frame_data = ThreadSafeBuffer()
self._frame_data = None
self._stop_all = Event()
self._server_thread = Thread(target=self._run_server, args=(host, port))
self._server_thread.daemon = True
Expand All @@ -123,7 +95,7 @@ def closed(self):
return self._stop_all.is_set()

def _run_server(self, host, port):
app.frame_data = self._frame_data
self._frame_data = app.frame_data
httpd = make_server(host, port, app, ws_handler_class=WebConsoleSocket)
while not self._stop_all.is_set():
try:
Expand Down
4 changes: 3 additions & 1 deletion web_pdb/wsgi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import bottle

from .buffer import ThreadSafeBuffer

__all__ = ['app']

# bottle.debug(True)
Expand Down Expand Up @@ -66,7 +68,7 @@ def wrapper(*args, **kwargs):
class WebConsoleApp(bottle.Bottle):
def __init__(self):
super().__init__()
self.frame_data = None
self.frame_data = ThreadSafeBuffer()


app = WebConsoleApp()
Expand Down

0 comments on commit a27dbdf

Please sign in to comment.