0

I've been given a Perl script implementation of a task by a third party product vendor. My job is to translate it into my ASP.NET MVC5 web application.

One component of the perl script is a while loop with this in it:

    ### Only run this task every second, that's plenty
    sleep 1;

Basically they're doing stuff in a while loop at 1 second intervals (pseudo code):

    While (condition)
    {
        -go get an m3u8 playlist file from the web
        -parse it line by line
        -look for a particular thing in it
        -If you find it, break out of this loop and do stuff with it
    }

It typically takes between 20-30 tries (20-30 seconds) to find the thing.

Is this the kind of thing asynchronous programming with async and await is geared toward or is this something one just shouldn't have in a web app?

The process would be exposed via ajax call to Web API. If it is feasible, could someone provide some pseudo code on how it might work without blocking the app?

2
  • By "do stuff with it" do you mean that the user will see, or it just updates the backend or something? Commented Sep 27, 2016 at 0:03
  • Just back end. Actually just drop the thing into the db. No data back to browser. Later the thing will be recalled from the db and used in another process. Commented Sep 27, 2016 at 14:36

2 Answers 2

1

If it is feasible, could someone provide some pseudo code on how it might work without blocking the app?

AJAX calls are always nonblocking. There's nothing special you need to do on the server side for this.

I would recommend using async/await, just so that you don't use up an ASP.NET thread sleeping:

public async Task<MyResult> GetThing()
{
  While (condition)
  {
    -go get an m3u8 playlist file from the web
    -parse it line by line
    -look for a particular thing in it
    -If you find it, break out of this loop and do stuff with it
    await Task.Delay(TimeSpan.FromSeconds(1));
  }
}

But note that the async is only used to free up a thread on the ASP.NET server side. It has no effect on the client whatsoever. The client makes the AJAX call, and asynchronously handles completion, regardless of the server's implementation.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use async/await, but thats more about synchroizing async behaviour. Your problem is a little different, you have a long running process which you can't block on, so you need a way to trigger it to start, providing updates, and then handling completion. You could use something like signalR for this

If you want to use ajax for this, then something like for your API...

StartJob()    -> return GUID
JobStatus(guid)   ->  either, InProgress ( % complete if you can), Final Result, or Error

then client side, Start it, then poll for status until complete.

Implementation wise, you just Start a task and store it in a cache associated with a GUID.

If you want to get fancy pants, you could use something like Microsoft Orleans or Akka to do the processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.