[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Does this code spawn lots of objects?

Ben Edwards

5/31/2007 11:49:00 AM

I have the following code:-

dbh = DBI.connect('*', '*', '*')
smtp = Net::SMTP.start('*')
sql = open( "gw.sql", "r" ).read
sth = dbh.execute( sql )
sth.fetch_hash do |row|
mail = MailFactory.new()
mail.to = "ben.edwards@ingenta.com"
mail.from = "ben.edwards@ingenta.com"
mail.subject = "file from ingenta"
mail.text = "Here is the file"
mail.attach( "./" + row["IDENTITYID"] + "_" + month + year + ".csv" );
smtp.send_message( mail.to_s(), mail.from, mail.to )
end
sth.finish
smtp.finish
dbh.disconnect

This code seems to cause a new object to be created for every
iteration of the iterator. However is it true that 'mail =
MailFactory.new()' causes the previous Mailfactory object not to have
a reference to it and therefore it can be garbage collected?

Is there a better way of coding the above?

Ben
--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles...
(email address this email is sent from may be defunct)

4 Answers

Robert Klemme

5/31/2007 12:00:00 PM

0

On 31.05.2007 13:49, Ben Edwards wrote:
> I have the following code:-
>
> dbh = DBI.connect('*', '*', '*')
> smtp = Net::SMTP.start('*')
> sql = open( "gw.sql", "r" ).read
> sth = dbh.execute( sql )
> sth.fetch_hash do |row|
> mail = MailFactory.new()
> mail.to = "ben.edwards@ingenta.com"
> mail.from = "ben.edwards@ingenta.com"
> mail.subject = "file from ingenta"
> mail.text = "Here is the file"
> mail.attach( "./" + row["IDENTITYID"] + "_" + month + year + ".csv" );
> smtp.send_message( mail.to_s(), mail.from, mail.to )
> end
> sth.finish
> smtp.finish
> dbh.disconnect
>
> This code seems to cause a new object to be created for every
> iteration of the iterator. However is it true that 'mail =
> MailFactory.new()' causes the previous Mailfactory object not to have
> a reference to it and therefore it can be garbage collected?
>
> Is there a better way of coding the above?

Probably not much room for improvement. One thing you can do for sure
is to use the block form of those various connections / IO's you are
using. And you can read in a complete file by doing File.read("ge.sql").

Probably you can also reuse the MailFactory (from, to, subject and text
don't change anyway). But I have no idea whether that will create any
improvement. And I also do not know that class so take this advice with
a grain of salt.

Another idea is to start a second thread for mail sending. Connect both
threads with a queue with limited size. That way you may use IO
resources more efficiently.

Kind regards

robert

Ben Edwards

5/31/2007 1:40:00 PM

0

On 31/05/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 31.05.2007 13:49, Ben Edwards wrote:
> > I have the following code:-
> >
> > dbh = DBI.connect('*', '*', '*')
> > smtp = Net::SMTP.start('*')
> > sql = open( "gw.sql", "r" ).read
> > sth = dbh.execute( sql )
> > sth.fetch_hash do |row|
> > mail = MailFactory.new()
> > mail.to = "ben.edwards@ingenta.com"
> > mail.from = "ben.edwards@ingenta.com"
> > mail.subject = "file from ingenta"
> > mail.text = "Here is the file"
> > mail.attach( "./" + row["IDENTITYID"] + "_" + month + year + ".csv" );
> > smtp.send_message( mail.to_s(), mail.from, mail.to )
> > end
> > sth.finish
> > smtp.finish
> > dbh.disconnect
> >
> > This code seems to cause a new object to be created for every
> > iteration of the iterator. However is it true that 'mail =
> > MailFactory.new()' causes the previous Mailfactory object not to have
> > a reference to it and therefore it can be garbage collected?
> >
> > Is there a better way of coding the above?
>
> Probably not much room for improvement. One thing you can do for sure
> is to use the block form of those various connections / IO's you are
> using. And you can read in a complete file by doing File.read("ge.sql").

What difference will this make?

Thanks for advice,
Ben

> Probably you can also reuse the MailFactory (from, to, subject and text
> don't change anyway). But I have no idea whether that will create any
> improvement. And I also do not know that class so take this advice with
> a grain of salt.
>
> Another idea is to start a second thread for mail sending. Connect both
> threads with a queue with limited size. That way you may use IO
> resources more efficiently.
>
> Kind regards
>
> robert
>
>


--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles...
(email address this email is sent from may be defunct)

Ben Edwards

5/31/2007 1:42:00 PM

0

On 31/05/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 31.05.2007 13:49, Ben Edwards wrote:
> > I have the following code:-
> >
> > dbh = DBI.connect('*', '*', '*')
> > smtp = Net::SMTP.start('*')
> > sql = open( "gw.sql", "r" ).read
> > sth = dbh.execute( sql )
> > sth.fetch_hash do |row|
> > mail = MailFactory.new()
> > mail.to = "ben.edwards@ingenta.com"
> > mail.from = "ben.edwards@ingenta.com"
> > mail.subject = "file from ingenta"
> > mail.text = "Here is the file"
> > mail.attach( "./" + row["IDENTITYID"] + "_" + month + year + ".csv" );
> > smtp.send_message( mail.to_s(), mail.from, mail.to )
> > end
> > sth.finish
> > smtp.finish
> > dbh.disconnect
> >
> > This code seems to cause a new object to be created for every
> > iteration of the iterator. However is it true that 'mail =
> > MailFactory.new()' causes the previous Mailfactory object not to have
> > a reference to it and therefore it can be garbage collected?
> >
> > Is there a better way of coding the above?
>
> Probably not much room for improvement. One thing you can do for sure
> is to use the block form of those various connections / IO's you are
> using. And you can read in a complete file by doing File.read("ge.sql").

Sorry, forgot to ask. What do you mean by block form?

> Probably you can also reuse the MailFactory (from, to, subject and text
> don't change anyway). But I have no idea whether that will create any
> improvement. And I also do not know that class so take this advice with
> a grain of salt.
>
> Another idea is to start a second thread for mail sending. Connect both
> threads with a queue with limited size. That way you may use IO
> resources more efficiently.
>
> Kind regards
>
> robert
>
>


--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles...
(email address this email is sent from may be defunct)

Robert Klemme

5/31/2007 1:45:00 PM

0

On 31.05.2007 15:42, Ben Edwards wrote:
> On 31/05/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 31.05.2007 13:49, Ben Edwards wrote:
>> > I have the following code:-
>> >
>> > dbh = DBI.connect('*', '*', '*')
>> > smtp = Net::SMTP.start('*')
>> > sql = open( "gw.sql", "r" ).read
>> > sth = dbh.execute( sql )
>> > sth.fetch_hash do |row|
>> > mail = MailFactory.new()
>> > mail.to = "ben.edwards@ingenta.com"
>> > mail.from = "ben.edwards@ingenta.com"
>> > mail.subject = "file from ingenta"
>> > mail.text = "Here is the file"
>> > mail.attach( "./" + row["IDENTITYID"] + "_" + month + year + ".csv" );
>> > smtp.send_message( mail.to_s(), mail.from, mail.to )
>> > end
>> > sth.finish
>> > smtp.finish
>> > dbh.disconnect
>> >
>> > This code seems to cause a new object to be created for every
>> > iteration of the iterator. However is it true that 'mail =
>> > MailFactory.new()' causes the previous Mailfactory object not to have
>> > a reference to it and therefore it can be garbage collected?
>> >
>> > Is there a better way of coding the above?
>>
>> Probably not much room for improvement. One thing you can do for sure
>> is to use the block form of those various connections / IO's you are
>> using. And you can read in a complete file by doing File.read("ge.sql").
>
> Sorry, forgot to ask. What do you mean by block form?

DBI.connect('*', '*', '*') do |dbh|
....
end

You gain safety because it's guaranteed that the connection is closed
regardless how the block is left (normal, exception).

Kind regards

robert