Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling (update mywidget) from a new thread #6

Open
bamboospirit opened this issue Jan 1, 2021 · 2 comments
Open

Calling (update mywidget) from a new thread #6

bamboospirit opened this issue Jan 1, 2021 · 2 comments

Comments

@bamboospirit
Copy link

How to make a new thread aware of the session it was created from ?

(:input :type "button" :value "Add random task (with (update) called from a different thread)" :onclick
		  (weblocks/actions:make-js-action
		   (lambda (&rest args )
		     (add-task task-list (format nil "~A" (random 1001)))
		     (bt:make-thread
		      (lambda ()
			(weblocks/dependencies:with-collected-dependencies 
			  (sleep 0.5)
			  (update task-list) ; ERROR: Session was not created for this request!
			  ))))))

complete executable code to illustrate the problem: https://gist.github.com/bamboospirit/e6ad95503ec40d2c7649f4aa14ad4979#file-thr-update-lisp

@svetlyak40wt svetlyak40wt changed the title calling (update mywidget) from a new thread Calling (update mywidget) from a new thread Jan 1, 2021
@svetlyak40wt
Copy link
Member

There are two problems with this code.

To make the session variable available in the new thread, you need to provide initial bindings to a new thread like that:

CL-USER> (let ((weblocks/session::*session* :test-value))
           (bt:make-thread (lambda ()
                             (format t "Session in thread: ~A~%"
                                     weblocks/session::*session*))))
Session in thread: NIL

but

CL-USER> (let ((weblocks/session::*session* :test-value))
           (bt:make-thread (lambda ()
                             (format t "Session in thread: ~A~%"
                                     weblocks/session::*session*))
                           :initial-bindings
                           (list* (cons 'weblocks/session::*session*
                                        weblocks/session::*session*)
                                  bt:*default-special-bindings*)))
Session in thread: TEST-VALUE

However, this will not solve the second issue:

When you'll call the update method in the thread, it will have no effect, because Weblocks already sent a response to the browser.

When you call to action and update is called the thread which processes the HTTP request, then a HTML with a new widget representation is sent in response to the action. With update in a separate thread, this will not work, because you need an additional channel to send an update to the client-side.

Currently, this task only can be done with a WebSocket.

@bamboospirit
Copy link
Author

Got it. https://github.com/40ants/weblocks-websocket did the job

@svetlyak40wt svetlyak40wt transferred this issue from 40ants/weblocks Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants