From: Bill Kelly Date: 2010-05-19T13:26:24+09:00 Subject: Re: Asynchronous HTTP request Tony Arcieri wrote: > On Tue, May 18, 2010 at 7:56 PM, Daniel DeLorme wrote: > >> What I can't figure out with EventMachine is how to have the "main >> thread" generate the layout while the subrequests are executing. > > The problem here is inversion of control. EventMachine inverts control on > you, and it sucks. You can't just do subreq(...) and expect it to return a > value. In the best case, you have subreq call a block when it completes. > The familiar pattern of "call function, get value" no longer applies. Sorry if I'm being redundant, but I wanted to point out that EventMachine *can* support non-inverted control semantics, with a Fiber-based wrapper layer. For example, the following is an excerpt from a test for a single-threaded EventMachine application. Most of the methods below are being invoked on remote server(s), but nothing ever blocks the thread. (Other Fibers on the same EM thread will still be responding to UI events, etc.) def test_add_documents_to_catalog @app.reset_to_init_state catalog1_path = @app.testsv_uri + URI.encode("/@default-catalog-path/catalog1") catalogs = @app.catalog_manager cat = catalogs.open_catalog(catalog1_path, :delete_existing=>true) num_docs = cat.query_num_documents assert_equal(0, num_docs) catalogs.active_catalog = cat assert_equal( cat, catalogs.active_catalog ) doc_paths = imageset_paths(1) + imageset_paths(2) per_dir_doc_paths = @app.partition_filelist_per_directory(doc_paths) records = @app.fetch_metadata_for_partitioned_filelist(per_dir_doc_paths) assert_equal( doc_paths.length, records.length ) # run the 'store' test twice, to make sure the # "INSERT OR REPLACE" is working... 2.times do cat.store_document_metadata(records) num_docs = cat.query_num_documents num_docs_expected = doc_paths.length assert_equal(num_docs_expected, num_docs) end # try some searches paths = cat.search("caption" => "World Series") assert_equal( 1, paths.length ) assert_equal( imageset_paths(2)[0], paths[0] ) # etc. end Anyway, dunno if this adds anything to the topic. Apologies if not. Regards, Bill