Asynchronous Operations
The functions URLFetchAsynchronous and URLSaveAsynchronous are used to download data in the background, while continuing to use Mathematica for evaluations. Asynchronous function also lets you execute user-defined functions when data is received.
URLFetchAsynchronous and URLSaveAsynchronous both cause events to be triggered that can be handled by functions. For a basic example, to download the contents of a file in the background and be notified when the download completes.
eventFunc[_, "statuscode", _] := CreateDialog[{TextCell["Finished"], DefaultButton[]}];
URLSaveAsynchronous["wolfram.com", FileNameJoin[{$TemporaryDirectory, "index.html"}],
eventFunc]
| Out[2]= |
Both URLFetchAsynchronous and URLSaveAsynchronous can raise an event called
that returns information about the data being downloaded and uploaded.
progress = {};
eventFunc[_, "progress", data_]:= AppendTo[progress, data];
URLSaveAsynchronous["http://exampledata.wolfram.com/10mb.dat
", FileNameJoin[{$TemporaryDirectory, "index.html"}],
eventFunc, "Progress"→True]
| Out[5]= |
The progress information can then be examined.
Part[progress, RandomInteger[Length[progress]]]
| Out[6]= |
This information says that
bytes had been downloaded of a total of
bytes, while
bytes were uploaded out of a total of
bytes to be uploaded.
When using URLFetchAsynchronous, the
event will return the content downloaded; if using URLSaveAsynchronous, an empty list will be returned by the
event.
eventFunc[_, "data", data_]:= Print[FromCharacterCode[data]]
URLFetchAsynchronous["wolfram.com", eventFunc]
| Out[8]= |
Both URLFetchAsynchronous and URLSaveAsynchronous raise an
event if something goes wrong during the connection.
errorFunc[_, "error", _] := CreateDialog[{TextCell["Error Downloading Page"],
DefaultButton[]}];
URLFetchAsynchronous["http://notreal", errorFunc]
| Out[1]= |
The asynchronous functionality makes it easy to create dynamic applications.
progress = 0.;
progressFunc[_, "progress", {now_, total_, _, _}]:= Quiet[progress = now/total]
URLFetchAsynchronous["http://exampledata.wolfram.com/5mb.dat", progressFunc, "Progress"→True];
Dynamic[ProgressIndicator[progress]]
| Out[14]= |
Mathematica's powerful pattern matching can be used to easily write an event handler that receives multiple events.
multiEvent[_, "headers", headers_] := Print["Headers: ", headers];
multiEvent[_, "cookies", cookies_] := Print["Cookies: ", cookies];
multiEvent[_, "statuscode", statuscode_] := Print["StatusCode: ", statuscode];
multiEvent[_, "error", error_] := Print["Error:", error];
URLFetchAsynchronous["http://wolframalpha.com", multiEvent];
The first argument passed to an event function returns the AsynchronousTaskObject that raised the event; this is useful for running multiple tasks in the background.
asyncEvent[obj_, "statuscode", statuscode_] := Print[obj, " status code:", statuscode];
asyncEvent[obj_, "error", errorMsg_] := Print[obj, ": ", errorMsg];
URLFetchAsynchronous["http://wolfram.com", asyncEvent]
URLFetchAsynchronous["http://wolframalpha.com", asyncEvent]
URLFetchAsynchronous["http://notreal", asyncEvent]
| Out[22]= |
| Out[23]= |
| Out[24]= |
