aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYingtong Li <runassudo@yingtongli.me>2017-03-24 00:28:27 +1100
committerYingtong Li <runassudo@yingtongli.me>2017-03-24 00:28:27 +1100
commita1ca469f781f88759e428120e17b33059279fbad (patch)
treebd36bcac9b2ed847807bfb63da3dfdccde2ff8ad
parent1060ec9a0fa946900eaed56463be49230cb9631c (diff)
Improve stability maybe?
-rw-r--r--pblive/__init__.py28
-rw-r--r--pblive/data.py20
2 files changed, 38 insertions, 10 deletions
diff --git a/pblive/__init__.py b/pblive/__init__.py
index 13ce055..d0ab26b 100644
--- a/pblive/__init__.py
+++ b/pblive/__init__.py
@@ -95,7 +95,9 @@ def socket_join(session_name):
session = pblive.data.sessions[session_name]
user = pblive.data.Admin(sid=flask.request.sid, session=session)
+ pblive.data.admins_lock.acquire()
pblive.data.admins[flask.request.sid] = user
+ pblive.data.admins_lock.release()
# Send initial screen
flask_socketio.emit('update', render_question_admin(session, session.question_num), room=flask.request.sid)
@@ -113,16 +115,18 @@ def socket_disconnect():
user.session.colours.append(user.colour)
# Relay change
- for _, other_user in pblive.data.users.items():
+ for _, other_user in pblive.data.iterate_users():
if other_user != user and other_user.session == user.session:
flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
if not other_user.colour:
flask_socketio.emit('update', flask.render_template('colour_picker.html', session=user.session), room=other_user.sid)
- for _, admin in pblive.data.admins.items():
+ for _, admin in pblive.data.iterate_admins():
if admin.session == user.session:
flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid)
+ pblive.data.users_lock.acquire()
del pblive.data.users[flask.request.sid]
+ pblive.data.users_lock.release()
@socketio.on('register')
def socket_register(colour_id, colour_name):
@@ -133,12 +137,12 @@ def socket_register(colour_id, colour_name):
user.session.colours.remove(user.colour)
# Relay change
- for _, other_user in pblive.data.users.items():
+ for _, other_user in pblive.data.iterate_users():
if other_user != user and other_user.session == user.session:
flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
if not other_user.colour:
flask_socketio.emit('update', flask.render_template('colour_picker.html', session=user.session), room=other_user.sid)
- for _, admin in pblive.data.admins.items():
+ for _, admin in pblive.data.iterate_admins():
if admin.session == user.session:
flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid)
@@ -156,10 +160,10 @@ def socket_answer(question_num, answer):
flask_socketio.emit('update', render_question(user, user.session, user.session.question_num), room=user.sid)
# Relay change
- for _, other_user in pblive.data.users.items():
+ for _, other_user in pblive.data.iterate_users():
if other_user.session == user.session:
flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
- for _, admin in pblive.data.admins.items():
+ for _, admin in pblive.data.iterate_admins():
if admin.session == user.session:
flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=admin.sid)
flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid)
@@ -183,12 +187,14 @@ def socket_goto_question(question_num):
user.session.questions[question_num].answerer = random.choice([other_user for _, other_user in pblive.data.users.items() if other_user.session == user.session and other_user.colour])
# Relay change
- for _, other_user in pblive.data.users.items():
+ for _, other_user in pblive.data.iterate_users():
if other_user.session == user.session and other_user.colour:
flask_socketio.emit('update', render_question(other_user, other_user.session, other_user.session.question_num), room=other_user.sid)
- for _, admin in pblive.data.admins.items():
+ flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
+ for _, admin in pblive.data.iterate_admins():
if admin.session == user.session:
flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
+ flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid)
@socketio.on('pass_question')
def socket_pass_question():
@@ -199,9 +205,11 @@ def socket_pass_question():
user.session.questions[user.session.question_num].answerer = random.choice([other_user for _, other_user in pblive.data.users.items() if other_user.session == user.session and other_user.colour])
# Relay change
- for _, other_user in pblive.data.users.items():
+ for _, other_user in pblive.data.iterate_users():
if other_user.session == user.session and other_user.colour:
flask_socketio.emit('update', render_question(other_user, other_user.session, other_user.session.question_num), room=other_user.sid)
- for _, admin in pblive.data.admins.items():
+ flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
+ for _, admin in pblive.data.iterate_admins():
if admin.session == user.session:
flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
+ flask_socketio.emit('update_left', render_sidebar(admin, user.session), room=admin.sid)
diff --git a/pblive/data.py b/pblive/data.py
index 94ae05a..62187e3 100644
--- a/pblive/data.py
+++ b/pblive/data.py
@@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import threading
+
server_ip = None
sessions = {}
@@ -117,3 +119,21 @@ def unique_answers_for_question(session, question_num):
else:
answers[user.answers[question_num]] = [user]
return answers
+
+class DummyLock:
+ def acquire(self):
+ pass
+ def release(self):
+ pass
+
+users_lock = DummyLock()
+def iterate_users():
+ users_lock.acquire()
+ yield from list(users.items())
+ users_lock.release()
+
+admins_lock = DummyLock()
+def iterate_admins():
+ admins_lock.acquire()
+ yield from list(admins.items())
+ admins_lock.release()
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.