[racket] Yield (was: plot-frame does not wait at end of process)
Thank you Robby, yes it helps.
Based on this, I gave a try at writing a small example that describes some
particularities of using yield or sync or creating a new eventspace with or
without a thread:
https://gist.github.com/Metaxal/5182719#file-gistfile1-rkt
The comments at the bottom describe the different implications.
Don't hesitate to refine it or to make comments, in particular on the new
eventspace, because I'm not sure why it works...
Laurent
On Sun, Mar 17, 2013 at 2:41 PM, Robby Findler
<robby at eecs.northwestern.edu>wrote:
> For this specific program, it would behave the same way. In general what
> yield means is "block on this evt, but while you're blocked handle GUI
> events". So you call it from the eventspace handler thread of some
> eventspace, then that eventspace can still handle events while the evt
> passed to yield is not ready.
>
> FWIW, in general using yield properly is hard because you have to call it
> from inside some event handler and then you get into a nested situation,
> something that it is easy to get wrong. That is, GUIs tend to work well and
> be not too hard to implement if you maintain the "handle one event
> completely before starting to handle another one" discipline. But that
> isn't right sometimes (modal dialogs are the classic example: when you
> choose the File|Open... menu item in DrRacket, for example, it has to get
> half-way into the handling of that event and then start handling other
> events (clicks on the file dialog) before returning. So what you usually do
> here is just lock every other thing in your app ...).
>
> hth,
> Robby
>
>
>
>
> On Sun, Mar 17, 2013 at 8:33 AM, Laurent <laurent.orseau at gmail.com> wrote:
>
>> In his second version (see below), Matthew was basically encapsulating
>> what you have written inside (yield ...).
>> Would you mind to explain what differences that makes? I've read the docs
>> but I'm still confused about `yield'.
>>
>> >> >> > #lang at-exp racket
>> >> >> > (require plot
>> >> >> > racket/gui/base)
>> >> >> > (plot-new-window? #t)
>> >> >> > (yield
>> >> >> > (thread
>> >> >> > (lambda ()
>>
>> >> >> > (let loop ()
>> >> >> > (let ((dummy (read)))
>> >> >> > (if (and (number? dummy) (zero? dummy))
>> >> >> > (void)
>> >> >> > (begin
>> >> >> > ;; queue a callback instead of `plot' directly:
>> >> >> > (queue-callback
>> >> >> > (lambda ()
>> >> >> > (plot (function (λ(x) (* x x)) -2 2))))
>> >> >> > (loop))))))))
>>
>> Thanks,
>> Laurent
>>
>>
>> On Sun, Mar 17, 2013 at 2:13 PM, Robby Findler <
>> robby at eecs.northwestern.edu> wrote:
>>
>>> That way has the property that each plot window has its own thread of
>>> execution, which is probably not necessary (I don't know if plot windows
>>> communicate or if that matters), but if it were me, I'd probably just make
>>> one extra thread, not N, and I'd put the reading into the extra thread not
>>> the GUI, like the below.
>>>
>>> #lang racket/gui
>>> (require plot)
>>> (plot-new-window? #t)
>>> (define (do-one)
>>> (define dummy (read))
>>> (cond
>>> [(and (number? dummy) (zero? dummy))
>>> #f]
>>> [else
>>> (queue-callback
>>> (λ ()
>>> (plot (function (λ (x) (* x x)) -2 2))))
>>> #t]))
>>>
>>> (when (do-one)
>>> (void
>>> (thread
>>> (λ ()
>>> (let loop ()
>>> (when (do-one)
>>> (loop)))))))
>>>
>>>
>>>
>>>
>>> On Sun, Mar 17, 2013 at 4:42 AM, Laurent <laurent.orseau at gmail.com>wrote:
>>>
>>>> So, does this means that Deren's program from
>>>> http://lists.racket-lang.org/users/archive/2012-April/051490.html
>>>> would then look like that:
>>>>
>>>> #lang racket
>>>> (require plot)
>>>> (plot-new-window? #t)
>>>> (let loop ()
>>>> (let ((dummy (read)))
>>>> (if (and (number? dummy) (zero? dummy))
>>>> (void)
>>>> (begin
>>>> (parameterize ([current-eventspace (make-eventspace)])
>>>> (plot (function (λ(x) (* x x)) -2 2)))
>>>> (loop)))))
>>>>
>>>> ?
>>>>
>>>> Laurent
>>>>
>>>>
>>>> On Sun, Mar 17, 2013 at 7:15 AM, Eli Barzilay <eli at barzilay.org> wrote:
>>>>
>>>>> On Thursday, Robby Findler wrote:
>>>>> > Also: I don't think that you need a parameter for this default. the
>>>>> > current-eventspace parameter would already do this job.
>>>>>
>>>>> IOW, Robby wants no keyword or parameter, just the default behavior,
>>>>> and if there's a problem with that then people would deal with it like
>>>>> with any other gui code. FWIW, I'm on that side too.
>>>>>
>>>>> --
>>>>> ((lambda (x) (x x)) (lambda (x) (x x))) Eli
>>>>> Barzilay:
>>>>> http://barzilay.org/ Maze is
>>>>> Life!
>>>>> ____________________
>>>>> Racket Users list:
>>>>> http://lists.racket-lang.org/users
>>>>>
>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130317/8408df96/attachment.html>