By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

Ruby/Python Continuations: Turning a block callback into a read()-method?

P: n/a
After failing on a yield/iterator-continuation problem in Python (see
below) I tried the Ruby (1.8.2) language first time on that construct:
The example tries to convert a block callback interface
(Net::FTP.retrbinary) into a read()-like iterator function in order to
virtualize the existing FTP class as kind of file system. 4 bytes max
per read in this first simple test below. But it fails on the second
continuation with ThreadError after this second continuation really
executing!? Any ideas how to make this work/correct?

(The question is not about the specific FTP example as it - e.g. about a
rewrite of FTP/retrbinary or use of OS tricks, real threads with polling
etc... - but about the continuation language trick to get the execution
flow right in order to turn any callback interface into an "enslaved
callable iterator". Python can do such things in simple situations with
yield-generator functions/ But Python obviously fails by a
hair when there is a function-context barrier for "yield". Ruby's
block-yield-mechanism seems to not at all have the power of real
generator-continuation as in Python, but in principle only to be that
what a normal callback would be in Python. Yet "callcc" seemes to be
promising - I thought so far :-( )

=== Ruby callcc Pattern : execution fails with ThreadError!? ===========
require 'net/ftp'
module Net

class FTPFile
def initialize(ftp,path)
@ftp = ftp
def read
if @iter
puts ""
puts "RETR "+@path
@ftp.retrbinary("RETR "+@path,4) do |block|
print "CALLBACK ",block,"\n"
callcc{|@iter| @flag=true}
if @flag
return block


ftp ="localhost",'user','pass')
ff =,'data.txt')

=== Output/Error ====

vs:~/test$ ruby ftpfile.rb
RETR data.txt

/usr/lib/ruby/1.8/monitor.rb:259:in `mon_check_owner': current thread
not owner (ThreadError)
from /usr/lib/ruby/1.8/monitor.rb:211:in `mon_exit'
from /usr/lib/ruby/1.8/monitor.rb:231:in `synchronize'
from /usr/lib/ruby/1.8/net/ftp.rb:399:in `retrbinary'
from ftpfile.rb:17:in `read'
from ftpfile.rb:33

=== Python Pattern : I cannot write down the idea because of a barrier ===

#### I tried a pattern like:
def open(self,ftppath,mode='rb'):
class FTPFile:
def iter_retr()
def callback(blk):
how-to-yield-from-here-as-iter_retr blk???
self.ftp.retrbinary("RETR %s" % self.relpath,callback)
def read(self, bytes=-1):


Feb 12 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.