# BackgroundRb and blocking analysis of progress

**URL:** <https://www.ruby-forum.com/t/backgroundrb-and-blocking-analysis-of-progress/122756>\
**Category:** Rails\
**Created:** [December 11, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/backgroundrb-and-blocking-analysis-of-progress/122756 "2007-12-11T20:56:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 11, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/backgroundrb-and-blocking-analysis-of-progress/122756/1 "2007-12-11T20:56:30Z")

</div>

Hi,

I’m using the BackgroundRb scheduler (http://  
[backgroundrb.rubyforge.org/172](http://backgroundrb.rubyforge.org/172)) w/ rails (I’m using the older version  
– not the 1.0 release announced yesterday).

My understanding of BackgroundRb is that it creates a separate thread  
(divorced from http request/response) that can run a long-running  
process (LRP).

The idea is to allow a rails application to periodically check on the  
status of the LRP and (potentially) provide updates on a web-page to a  
user.

There is a bit of sample-code for BackgroundRb ([http://www.infoq.com/](http://www.infoq.com/)  
articles/BackgrounDRb) that demonstrates how the status of a LRP can  
be checked.

Here is sample-code from a worker class described in the link shown  
above:

class FooWorker \< BackgrounDRb::Rails  
attr\_reader :progress  
def do\_work(args)  
@progress = 0  
calculate\_the\_meaning\_of\_life(args)  
end  
def calculate\_the\_meaning\_of\_life(args)  
while @progress \< 100

# calculations here

@progress += 1  
end  
end  
end

FooWorker functions properly in my hands (i.e. a sample web-page will  
gradually show an increase in the value of @progress up to 100%).

The problem, however, happens when I change the code in the method  
called calculate\_the\_meaning\_of\_life(args) found within FooWorker to  
have it write a large file. The idea is to have rails/the-web-page  
check on the status of the file-writing until it is completely done.

The problem is that the file-writing method seems to lock the  
@progress variable and I can’t access @progress via my controller/  
periodical-updater until the entire  
method (calculate\_the\_meaning\_of\_life(args)) is complete.

The method shown below (where I write a file) doesn’t give me access  
to @progress until the file is completely written & closed.

This is part of my new FooWorker class file  
1 def calculate\_the\_meaning\_of\_life(args)  
2 e = Event.find\_all\_by\_id(best, :limit =\> 500000)  
3 @progress = 0.0  
4 @file\_name = “#{args[:user\_name]}.csv”  
5 open(“/e1/public/csvs/#{@file\_name}”, “w”)  
6 e.each\_with\_index do |row, i|  
7 f.puts((columns.map {|c| row.send(c[:accessor])}).join(“$”))  
8 @progress = 100.0 \* (i.to\_f / e.size.to\_f)  
9 end  
10 end  
11 @progress = 100  
12 end  
13 FooWorker.register

if I delete lines 5 and 10 (i.e. I remove the file-writing commands,  
then I have access to the @progress variable even while I’m iterating  
through the result set (e.each\_with\_index {|blah| blah blah }), but  
lines 5 and 10 block access.

Clearly, I’m doing something wrong (perhaps using the wrong file i/o  
method), but I’m not sure of how to fix the problem.

Any suggestions or explanations as to what I’m doing wrong are much  
appreciated.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 12, 2007, 9:52am UTC](https://www.ruby-forum.com/t/backgroundrb-and-blocking-analysis-of-progress/122756/2 "2007-12-12T09:52:43Z")

</div>

figured it out.

f.flush is needed.

On Dec 11, 2:38 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
