I want to launch an asynchronous task with Wolfram Script to listen to a socket and print back information in the CLI. This was handled in previous versions by ZeroMQLink``Private``$AsyncState["Task"]
but this doesn't appear to work with my version (13, Win 11 x64).
Minimal Working Example:
#!/usr/bin/env wolframscript
(* ::Package:: *)
Quiet@DeleteObject[listener];
listener = SocketListen[
3000,
Function[{assoc},
With[{client = assoc["SourceSocket"], data = assoc["Data"]},
request =
ImportString[data, "HTTPRequest"]["Query"] /. {"name" -> n_} :> n;
origin = Association[ request["Headers"] ]["origin"];
If[ Head[origin] === Missing, origin = "" ];
response = ExportString[
HTTPResponse[
ExportString[<|"msg" -> "Hello, " <> ToString[request]|>, "JSON"],
<|
"StatusCode" -> 200,
"ContentType" -> "application/json",
"Headers" -> { "Access-Control-Allow-Origin" -> origin }
|>], "HTTPResponse"];
WriteString[client, response];
Close[client];
Print[DateString[] <> " - " <> "Request=" <> ToString[request]];
]
]
]
url = URLBuild[<|"Scheme" -> "http",
"Domain" -> First[listener["Socket"]["DestinationIPAddress"]],
"Port" -> listener["Socket"]["DestinationPort"]|>];
Print["Listening: ", url, "\n"];
And to test in Powershell:
PS (Invoke-WebRequest -Method GET -Uri http://127.0.0.1:3000?name=kale).Content
Should return:
{
"msg":"Hello, kale"
}
but exits out as soon as the script finishes executing.
How can I create this socket through WolframScript and keep it running until the script is closed using the new Background & Scheduled Tasks framework in Wolfram Language?
ZeroMQLink`PackageScope`$AsyncState
but it doesn't have the"Task"
key, so even its"Status"
key is incorrect because it needs"Task"
. Although there isZeroMQLink`Loop`PackagePrivate`$backgroundTask
which seems to be the right one, in my testing on your case, I couldn't respond to the client. $\endgroup$TaskWait
, but yeah, clearly not responding to client. I snooped around quite a bit, but couldn't find that private package. How did you stumble on it? $\endgroup$Names["ZeroMQLink*`*task*", IgnoreCase -> True]
and also"ZeroMQLink*`*async*"
. It's interesting that even usingFirst@AsynchronousTasks[]
which I think should contain"Task"
in previous versions, shows the same results (not responding). If you want to explore other possible solutions, you may take a look at Use function defined in Mathematica from Matlab (the Update section). I used a third-partyZeroMQ
Java library. With some manipulation, you can build your own. $\endgroup$