Why this F# code does not generate expected output when used with MailboxProcessor? - f#

I was going through one of Don Syme's blog posts Async and Parallel Design Patterns in F#: Agents. However, the following seemingly extremely simple code did not generate output as expected.
type Agent<'T> = MailboxProcessor<'T>
let agent =
Agent.Start(fun inbox ->
async { while true do
let! msg = inbox.Receive()
printfn "got message '%s'" msg } )
for i in 1 .. 10000 do
agent.Post (sprintf "message %d" i)
Instead of expected 10,000 messages , I only got something around 3000 messages using Mono 2.8.1 under Ubuntu, or 15 messages using Visual F# under Windows XP. Am I missing anything here? BTW, I tried to replace the printfn statement with the following File op and ended up with same partial results.
open System.IO
type Agent<'T> = MailboxProcessor<'T>
let agent =
Agent.Start(fun inbox ->
async { while true do
let! msg = inbox.Receive()
use logger = new StreamWriter("a.log", true)
logger.WriteLine("got message '{0}'", msg.ToString())
logger.Close()
} )
for i in 1 .. 10000 do
agent.Post (sprintf "message %d" i)

Just run your code in Win machine - everything is OK. Try to add
ignore( System.Console.ReadKey() )
as a last line, because agent.Post is non-blocking and after posting 10000 messages control flow will move forward, possibly exiting the program.

Related

F# - Playwright return randomly result

I am using Playwright in F# for web scrapping and I noticed that result is returned randomly.
I have this code.
let getContent (url:string) =
task{
use! paywright = Playwright.CreateAsync()
let! browser = paywright.Chromium.LaunchAsync()
printfn "URL %A" url
let! page = browser.NewPageAsync()
page.SetDefaultTimeout(15000f)
let! goto = page.GotoAsync(url)
let! price = page.Locator("//span[#class='norm-price ng-binding']").AllInnerTextsAsync()
printfn "Price %A" price
}
When I run the console program sometimes it returns result (list of prices), but sometimes its just finished with empty result.
I really dont know what can be wrong. I also try use async wrapper instead of task but the output is same.
The delay I increase to 15s, but it also doesnt help.
Could it be that you do not await the task returned by getContent?
Maybe the program terminates before writing to the console. If the calling code is not asynchronous (and cannot propagate the task), you could try:
let printContent (url : string) =
task { ... } |> Task.RunSynchronously
Update 1:
Probably the page loads it's price data asynchronously.The default timeout on the page is there to specify a maximum timeout, not to wait that long for some data to arrive in the controlled browser instance. Most likely you'll have to wait for some request to finish or some element to appear on the page. Can you share the URL publicly?

Try..With Block is hanging after the First Error F#

I am fairly new to F# and my code is "hanging" after the first instance of an error. It just does nothing really.
This is at the very end of a function the call is a SOAP call to an API endpoint.
try
let a = client.catalogProductCreateAsync(sessionId, "simple","4",priceListItem.sku, productData, "").Result
printfn "%A" a
with ex ->
printfn "%A" ex
"" // blank string returned - not relevant to the above - it's just the end of the funciton

Unit Testing F# Code Throws Null Reference Exception, why?

I'm using the NUnit testing technique suggested in the yet to be released book "F# Deep Dives Version 12" (Sec. 2.2 "Adding Tests")
The code below executes fine compiled or interactive with MEMOIZE defined/undefined. However, executing the unit test from the GUI NUnit works fine with MEMOIZE undefined, but it fails with a "Null Reference Exception" when MEMOIZE is defined. Notice memorize() uses a Closure. I'm suspecting the exception is happening because some initialization code generated by the compiler is not getting executed when NUnit starts up. What do you think?
open System
open System.Collections.Generic
open NUnit.Framework
open FsUnit
let memoize (f: 'T -> 'U) =
let t = new Dictionary<'T, 'U>(HashIdentity.Structural)
fun n ->
if t.ContainsKey n then t.[n]
else let res = f n
t.Add(n, res)
res
//TODO: Insure J>0 & K>0 & J<K
let isMult =
#if MEMOIZE
memoize (fun (j,k) -> k % j = 0)
#else
(fun (j,k) -> k % j = 0)
#endif
type ``Given the isMult function``() =
[<TestCase(3,1,false)>]
[<TestCase(3,2,false)>]
[<TestCase(3,3,true)>]
[<TestCase(5,10,true)>]
[<TestCase(3,15,true)>]
[<TestCase(5,13,false)>]
[<TestCase(5,15,true)>]
member t.``the result is calculated correctly``(j, k, expected) =
let actual = isMult (j,k)
actual |> should equal expected
UPDATE:
The standalone NUnit application is version 2.6.3.13283.
"FinnNk" gave me an idea! I installed Nuget package "NUnitTestAdapter". Now I can test directly within VS 2013. No surprises, though. I get all tests 'green' when MEMORIZE is undefined and all tests 'red' when it is defined.
The exception is still the same: "Null Reference Exception". However, now that it executes in the IDE, I can have the debugger stop on the exception. All I can determine so far at the breakpoint is that it needs the symbols from:
C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.pdb
I installed the new VS 2015 Preview Edition. Nothing different happens in that environment. Now that .NET Framework is open source, maybe I can zero the debugger precisely on the problem with the source code for "mscorlib".
Are you running your NUnit tests in multiple threads? Normal dictionary is not thread-safe so weird things can happen. How about if you use ConcurrentDictionary, will it give the same result?
let memoize (f: 'T -> 'U) =
let t = System.Collections.Concurrent.ConcurrentDictionary<'T, 'U>()
fun n -> t.GetOrAdd(n, f)

WebSharper force a message passing call to be asynchronous

Knowing an RPC call to a server method that returns unit is a message passing call, I want to force the call to be asynchronous and be able to fire the next server call only after the first one has gone to the server.
Server code:
[<Rpc>]
let FirstCall value =
printfn "%s" value
async.Zero()
[<Rpc>]
let SecondCall() =
"test"
Client code:
|>! OnClick (fun _ _ -> async {
do! Server.FirstCall "test"
do Server.SecondCall() |> ignore
} |> Async.Start)
This seems to crash on the client since returning unit, replacing the server and client code to:
[<Rpc>]
let FirstCall value =
printfn "%s" value
async { return () }
let! _ = Server.FirstCall "test"
Didn't fix the problem, while the following did:
[<Rpc>]
let FirstCall value =
printfn "%s" value
async { return "" }
let! _ = Server.FirstCall "test"
Is there another way to force a message passing call to be asynchronous instead?
This is most definitely a bug. I added it here:
https://bugs.intellifactory.com/websharper/show_bug.cgi?id=468
Your approach is completely legit. Your workaround is also probably the best for now, e.g. instead of returning Async<unit> return Async<int> with a zero and ignore it.
We are busy with preparing the 2.4 release due next week and the fix will make it there. Thanks!
Also, in 2.4 we'll be dropping synchronous calls, so you will have to use Async throughout for RPC, as discussed in https://bugs.intellifactory.com/websharper/show_bug.cgi?id=467 -- primarily motivated by new targets (Android and WP7) that do not support sync AJAX.

Is there a better way to write named-pipes in F#?

I am new to F#. I am trying to communicate with java from F# using named pipe. The code below works but I am not sure if there is a better way to do this (I know the infinite loop is a bad idea but this is just a proof of concept) if anyone have any idea to improve this code please post your comments.
Thanks in advance
Sudaly
open System.IO
open System.IO.Pipes
exception OuterError of string
let continueLooping = true
while continueLooping do
let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
printfn "[F#] NamedPipeServerStream thread created."
//wait for connection
printfn "[F#] Wait for a client to connect"
pipeServer.WaitForConnection()
printfn "[F#] Client connected."
try
// Stream for the request.
let sr = new StreamReader(pipeServer)
// Stream for the response.
let sw = new StreamWriter(pipeServer)
sw.AutoFlush <- true;
// Read request from the stream.
let echo = sr.ReadLine();
printfn "[F#] Request message: %s" echo
// Write response to the stream.
sw.WriteLine("[F#]: " + echo)
pipeServer.Disconnect()
with
| OuterError(str) -> printfn "[F#]ERROR: %s" str
printfn "[F#] Client Closing."
pipeServer.Close()
Well, it doesn't look like anything is throwing OuterError, so I would remove that exception type and unused handling.
I am unsure about your experience level or what type of "better" you are looking for. You way want to read F# async on the server to learn more about async and avoiding blocking threads.
Below you can find a few modifications to your code. Your question is pretty vague so I can't tell exactly where you're wishing to improve your code, but my suggestion uses recursion instead of the while loop (don't worry about stack overflows, F# can handle recursion very well and the whole recursive bit will be optimized into a loop at compile time), makes use of the use keyword (like C#'s using) and will swallow any exception happening in the process of the communication with the client. If an exception occurs, the server will not listen for other connections.
open System.IO
open System.IO.Pipes
let main() =
printfn "[F#] NamedPipeServerStream thread created."
let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4)
let rec loop() =
//wait for connection
printfn "[F#] Wait for a client to connect"
pipeServer.WaitForConnection()
printfn "[F#] Client connected."
try
// Stream for the request.
use sr = new StreamReader(pipeServer)
// Stream for the response.
use sw = new StreamWriter(pipeServer, AutoFlush = true)
// Read request from the stream.
let echo = sr.ReadLine();
printfn "[F#] Request message: %s" echo
// Write response to the stream.
echo |> sprintf "[F#]: %s" |> sw.WriteLine
pipeServer.Disconnect()
if [A CONDITION WHICH TELLS YOU THAT YOU WANT ANOTHER CONNECTION FROM THE CLIENT] then loop()
with
| _ as e -> printfn "[F#]ERROR: %s" e.Message
loop()
printfn "[F#] Client Closing."
pipeServer.Close()
Also please notice how the AutoFlush is set within the call to the constructor and how the pipeline operator is used to write the echo to the pipe, resulting in what looks (in my opinion) like cleaner code.

Resources