Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
d414a68c
Commit
d414a68c
authored
4 years ago
by
Mario Raciti
Browse files
Options
Downloads
Patches
Plain Diff
TMSS-413
: Add asyncio event loop to run websocket server as a thread
parent
e66c7cac
No related branches found
No related tags found
1 merge request
!282
Resolve TMSS-417
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
SAS/TMSS/services/websocket/lib/websocket_service.py
+15
-7
15 additions, 7 deletions
SAS/TMSS/services/websocket/lib/websocket_service.py
with
15 additions
and
7 deletions
SAS/TMSS/services/websocket/lib/websocket_service.py
+
15
−
7
View file @
d414a68c
...
...
@@ -32,6 +32,7 @@ from lofar.sas.tmss.client.tmssbuslistener import *
import
asyncio
import
socketio
from
aiohttp
import
web
from
threading
import
Thread
class
WebsocketServer
():
...
...
@@ -45,11 +46,11 @@ class WebsocketServer():
self
.
app
=
web
.
Application
()
self
.
sio
.
attach
(
self
.
app
)
def
start
(
self
):
def
start
(
self
,
loop
):
# Start a websocket server
asyncio
.
set_event_loop
(
loop
)
# FIXME: set_wakeup_fd only works in main thread.
web
.
run_app
(
self
.
app
,
host
=
'
127.0.0.1
'
,
port
=
5678
)
def
stop
(
self
):
# TODO:
def
stop
(
self
):
# TODO: Gracefully shutdown the server
# self.app.shutdown()
# self.app.cleanup()
pass
...
...
@@ -61,7 +62,7 @@ class WebsocketServer():
@staticmethod
@sio.event
async
def
message
(
sid
,
data
):
async
def
message
(
sid
,
data
):
# Just for debugging
logger
.
info
(
'
Received: %s
'
%
data
)
await
WebsocketServer
.
sio
.
emit
(
'
broadcastNotify
'
,
{
'
msg
'
:
'
Broadcast notify.
'
})
...
...
@@ -82,11 +83,18 @@ class TMSSEventMessageHandlerForWebsocket(TMSSEventMessageHandler):
'''
'''
def
__init__
(
self
)
->
None
:
self
.
_ws
=
WebsocketServer
()
# TODO: create websocket
self
.
_ws
=
WebsocketServer
()
super
().
__init__
()
def
start_handling
(
self
):
self
.
_ws
.
start
()
# TODO: Run the server in a separated thread
# self._ws.start() # TODO: Run the server in a separated thread
# FIXME:
# If include lines 94-95 -> ValueError: set_wakeup_fd only works in main thread.
# If comment lines 94-95 and run _ws.start() without loop -> RuntimeError: There is no current event loop in thread 'Thread-1'.
loop
=
asyncio
.
new_event_loop
()
asyncio
.
set_event_loop
(
loop
)
self
.
_thread
=
Thread
(
target
=
self
.
_ws
.
start
,
args
=
(
loop
,),
daemon
=
True
)
self
.
_thread
.
start
()
super
().
start_handling
()
def
stop_handling
(
self
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment