In Racket GUI applications, place-channel-get is a blocking operation—it waits until a message is received on the channel. If you call it directly in the main GUI thread (e.g., in an event handler), it will freeze the GUI until a message arrives.
Solution: Use thread + queue-callback
To avoid blocking the GUI, you should:
- Run
place-channel-get in a separate thread (so it doesn't block the main thread).
- Use
queue-callback to process the received message in the GUI thread (since GUI operations must run in the main thread).
Example: Non-Blocking place-channel-get with queue-callback
Here is a complete, runnable example demonstrating the correct pattern.
#lang racket/gui
(require racket/place)
;; --- GUI Setup ---
(define frame (new frame% [label "Place Channel Listener"] [width 400] [height 200]))
(define listener-status
(new message%
[parent frame]
[label "Status: Waiting for a message..."]
[auto-resize #t]))
(define received-message
(new message%
[parent frame]
[label "Last received message: None"]
[auto-resize #t]))
(send frame show #t)
;; --- Place and Channel Setup ---
;; This place simply waits for a moment and then sends a message.
(define listener-place
(place
(λ ()
(sleep 3) ; Simulate some work
(define ch (place-channel))
(place-channel-put ch "Hello from the place!")
(printf "Place has sent the message.\n"))))
;; We only need the channel from the place to listen.
(define ch (place-channel listener-place))
;; --- Non-Blocking Listener ---
;; This code runs in the main GUI setup phase.
(thread
(λ ()
;; This is a background thread. It will not freeze the GUI.
(printf "Listener thread started. Waiting for a message...\n")
(let ([msg (place-channel-get ch)]) ; This call blocks, but safely in the background.
(printf "Listener thread received: '%s'\n" msg)
;; Now, we must update the GUI. We use queue-callback to run the update
;; code in the main GUI thread.
(queue-callback
(λ ()
(send listener-status set-label "Status: Message received!")
(send received-message set-label (format "Last received message: '~a'" msg))
(send frame refresh)))))) ; Refresh to ensure layout updates if needed.
How It Works
- GUI Initialization: The GUI is created and displayed. The labels show their initial "waiting" state.
- Place Creation: A
place is created. Inside this place, a message is sent after a 3-second delay.
- Background Thread: A new
thread is started immediately.
- This thread executes the lambda function.
- It calls
(place-channel-get ch), which blocks. However, because this is a background thread, the main GUI thread remains free and responsive. You can still move, resize, and interact with the window.
- Message Reception: After 3 seconds, the
place sends the message. The place-channel-get call in the background thread unblocks and receives the string "Hello from the place!".
- Scheduling the GUI Update: The background thread now has the message but cannot touch the GUI directly. It calls
(queue-callback ...)
queue-callback takes a procedure and schedules it to be run by the main GUI thread's event loop at its next opportunity.
- The procedure it provides uses
send to update the labels on the GUI.
- GUI Update: The main GUI thread, when it processes its event queue, executes the callback. The labels are updated, and the GUI reflects the new state.
Alternative: Request-Response with sync
If you need to send a request and wait for a response (like a remote procedure call), you can combine place-channel-put and place-channel-get using sync. However, you must still run the entire sync operation in a background thread to prevent GUI freezing.
;; Example of a request-response in a background thread
(thread
(λ ()
(define request-ch (make-place-channel))
(define response-ch (make-place-channel))
;; Send a request and wait for the response channel to be returned
(place-channel-put some-other-place
(list 'request-data "some-payload" response-ch)))
;; Now wait for the actual data on the response channel
(define result (sync (λ () (place-channel-get response-ch))))
(queue-callback (λ () (update-gui-with result)))))
Best Practices Summary
- ✅ Do use
thread for any blocking operation (place-channel-get, file I/O, network requests).
- ✅ Do use
queue-callback to safely update GUI elements from a background thread.
- ❌ Don't call
place-channel-get or any other blocking function directly in a GUI event handler or top-level GUI code.
- ❌ Don't try to update GUI elements (e.g., using
send) from a background thread directly. This will cause errors or unpredictable behavior.
Conclusion: Yes, combining thread with queue-callback is the idiomatic and correct way to listen for place-channel-get messages without blocking your Racket GUI application.