Re: [racket-users] How to limit memory usage and raise out-of-memory error?

2018-08-10 Thread Shu-Hung You
Cool! That does the job. Thank you.

On Fri, Aug 10, 2018 at 3:57 PM, Matthew Flatt  wrote:
> For the second part, use `thread/suspend-to-kill` to create a thread
> that is merely suspended when its custodian is shut down, and then you
> can use `(continuation-marks thread-id)` to get the thread's
> continuation marks at the point where it was suspended.
>
> You'll need to wait until either the thread is terminated or the
> custodian is shutdown, instead of waiting until the thread terminates.
> Use a custodian box (for the same custodian) as a synchronizable event
> for custodian shutdown.
>
> At Fri, 10 Aug 2018 15:27:07 -0500, Shu-Hung You wrote:
>> I'm running a thread with memory limit using custodian-limit-memory.
>> How do I reliably detect whether the thread I'm running was terminated
>> normally or aborted by custodian shutdown? Plus, is it possible to
>> obtain the context at the time the memory limit was exceeded?
>>
>> I can think these two:
>>
>> 1. Use a channel to indicate normal termination, or
>> 2. detect whether the custodian had been shut down.
>>
>> Both methods address the first question but not the second question.
>> Any pointers?
>>
>> Best,
>> Shu-Hung
>>
>> (define TIMEOUT 60)
>> (define MEMORY-LIMIT (* 2 1024 1024 1024))
>>
>> (parameterize ([current-custodian (make-custodian)])
>>   (custodian-limit-memory (current-custodian) MEMORY-LIMIT)
>>   (define thread-id
>> (thread (λ ()
>>   (displayln "This displayln probably takes lots of 
>> memory"
>>   (define ended? (sync/timeout/enable-break TIMEOUT thread-id))
>>   (unless ended? (break-thread thread-id 'terminate))
>>   (thread-wait thread-id)
>>   (when (custodian-shut-down? (current-custodian))
>> ;; The context here is not the right one and the exception is not
>> ;; raised in the right thread
>> (raise (exn:fail:out-of-memory "Out of memory"
>> (current-continuation-marks)))
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] How to limit memory usage and raise out-of-memory error?

2018-08-10 Thread Matthew Flatt
For the second part, use `thread/suspend-to-kill` to create a thread
that is merely suspended when its custodian is shut down, and then you
can use `(continuation-marks thread-id)` to get the thread's
continuation marks at the point where it was suspended.

You'll need to wait until either the thread is terminated or the
custodian is shutdown, instead of waiting until the thread terminates.
Use a custodian box (for the same custodian) as a synchronizable event
for custodian shutdown.

At Fri, 10 Aug 2018 15:27:07 -0500, Shu-Hung You wrote:
> I'm running a thread with memory limit using custodian-limit-memory.
> How do I reliably detect whether the thread I'm running was terminated
> normally or aborted by custodian shutdown? Plus, is it possible to
> obtain the context at the time the memory limit was exceeded?
> 
> I can think these two:
> 
> 1. Use a channel to indicate normal termination, or
> 2. detect whether the custodian had been shut down.
> 
> Both methods address the first question but not the second question.
> Any pointers?
> 
> Best,
> Shu-Hung
> 
> (define TIMEOUT 60)
> (define MEMORY-LIMIT (* 2 1024 1024 1024))
> 
> (parameterize ([current-custodian (make-custodian)])
>   (custodian-limit-memory (current-custodian) MEMORY-LIMIT)
>   (define thread-id
> (thread (λ ()
>   (displayln "This displayln probably takes lots of 
> memory"
>   (define ended? (sync/timeout/enable-break TIMEOUT thread-id))
>   (unless ended? (break-thread thread-id 'terminate))
>   (thread-wait thread-id)
>   (when (custodian-shut-down? (current-custodian))
> ;; The context here is not the right one and the exception is not
> ;; raised in the right thread
> (raise (exn:fail:out-of-memory "Out of memory"
> (current-continuation-marks)))
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to limit memory usage and raise out-of-memory error?

2018-08-10 Thread Shu-Hung You
I'm running a thread with memory limit using custodian-limit-memory.
How do I reliably detect whether the thread I'm running was terminated
normally or aborted by custodian shutdown? Plus, is it possible to
obtain the context at the time the memory limit was exceeded?

I can think these two:

1. Use a channel to indicate normal termination, or
2. detect whether the custodian had been shut down.

Both methods address the first question but not the second question.
Any pointers?

Best,
Shu-Hung

(define TIMEOUT 60)
(define MEMORY-LIMIT (* 2 1024 1024 1024))

(parameterize ([current-custodian (make-custodian)])
  (custodian-limit-memory (current-custodian) MEMORY-LIMIT)
  (define thread-id
(thread (λ ()
  (displayln "This displayln probably takes lots of memory"
  (define ended? (sync/timeout/enable-break TIMEOUT thread-id))
  (unless ended? (break-thread thread-id 'terminate))
  (thread-wait thread-id)
  (when (custodian-shut-down? (current-custodian))
;; The context here is not the right one and the exception is not
;; raised in the right thread
(raise (exn:fail:out-of-memory "Out of memory"
(current-continuation-marks)))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.