Forums

Is it impossible to create real time view?

Hello,

Lately i have been trying to create my asynchronous view, where, Bot would update user in progress by websockets. However, Since websockets do not work, In previous question i was recommended to use Pusher.

But now, I definitely needed parallel execution, But now i find out that multithreading doesn't work... Neither does third party application like Celery (which is quite heavyweight and i'm trying to avoid it).

I'm not even sure if this is possible anymore, I'm getting limited to create this view no matter what i use... Is there any way that i can create another thread at exact time as template is loaded without using too complicated and heavyweight ways?

You could have a separate view that knows how far the process has progressed and then poll it periodically from your javascript.

@glenn Thanks for the response, Do i need to define seperate request view or function? I'm really trying to avoid GET/POST requests for this action for safety reasons.

If you want to interact with the server, you'll need to use either a GET or a POST. That's how http servers communicate. For getting a progress bar you just need GET. What safety reasons?

@Glenn I meant using only websockets for this realtime communication, If i used POST/GET requests other users could simply monitor Http traffic and use them too. With websockets and multithreading i could do all of it on single request. Also how could i run two request functions together? I'm not quite sure if i can create asychronous contact between two request functions.

EDIT:

Otherwise, Can i use normal TCP socket to communicate with other server which will give the proxy? Is socket module allowed for paid users?

You're worried about someone overhearing a progress report, but not the entire rest of your site?

You'd need some way that is not process-specific (like a file or database) to communicate between the 2 views.

Raw sockets will not work because we don't route random ports into any of the PythonAnywhere servers.

@Glenn So basically it is impossible to create a smooth single request live view correct? Whenever user sends the request to receive template and runs script, Don't they both need to be loaded on the same thread before response is sent to the user? I'm sorry, I just can't imagine other way of doing smooth progress updation without multithreading...

Hi there, can you tell us a bit more about exactly what it is you're trying to do? What information do you want to show the user on the page when it first loads, and what is it that you want to show a progress bar for?

Your constraints are: - you can't use websockets - you can't use threads - you can't use a raw TCP socket connection

but there are lots of things you can do within the simple HTTP(S) request/response model. external services like Pusher might also help.

Hello Harry,

Thanks for the response, I'm trying to make an asynchronous response, So -

  1. User fills the specific form and submits it.
  2. On form submission, Certain script will be executed and template will be rendered. (I'm trying to make it so, That both of these actions are done at the same time, Before script will finish all of the actions, Template will show loading page.
  3. While script is doing certain actions, It must also somehow notify Javascript in that template with the progress. Lets define progress as P variable.
  4. The paragraphs in the template will be displayed and hidden depending on value of P variable. E.g If P == 0, Paragraph with the text node "Loading" will be displayed, Else if P == 1, Paragraph with text node "Loading" will be hidden and new paragraph with text node "Sending the Trade" will be displayed.
  5. Finally, Once the script is finished, It will simply send variable P with the value of 2 (which in this case, Is final step) and Javascript will make redirection to certain page.

I'm not quite sure how could i make this asynchronous actions, Is there any other way of doing this without using multithreading/websockets? Thanks in advance.

I would recommend something like this:

  • set up a database table to hold information about the jobs you want to do ansynchronously
  • in your web app, when the user submits a form, add a new job to the table
  • create a sheduled task whose job it is to poll the database table and look for new jobs (maybe an infinite loop that sleeps for 10s if it can't find any jobs. see also this page)
  • the task can also publish progress information, perhaps to the same table
  • to update the user with progress information, create a separate view that you can poll using ajax, that retrieves progress information from the table
  • on the client-side, you can poll this ajax view using javascript to display a progress bar.

@harry Thanks a lot for the concept, So this is what i'm currently going to do:

What i have to do manually:

  • Create folder progress_reports (Is new database initialization necessary?)
  • Create scheduled task which will sleep 10s and monitor if new files are created inside progress_reports folder.

If there is new json file with certain data, Script will do all the necessary stuff and also update <progress_id>.json with new progress.

After user submits form:

  • In the views.py generate random string progress_id
  • In the views.py create a file <progress_id>.json
  • Add necessary information for the script inside <progress_id>.json
  • Send the render response to the user and add new key progress_id to dict type context.
  • Use Ajax to monitor {{progress_id}}.json file - https://stackoverflow.com/questions/7028410/monitor-file-change-through-ajax-how
  • Finally change the style's of div elements according to the progress report.

Is everything listed possible on pythonanywhere? (Sorry for being annoying by the way, This is my final question in this topic, If everything listed is possible).

Thanks a lot.

That sounds like it could work! So you're planning on making the <progress id>.json files available directly, rather than writing an extra view? That's neat. Check out our static files docs if you haven't already.

@harry So it's better to progress_files folder to /static/ directory correct? Also in the future, Could i decrease down my long running task sleeping process if i purchase better premium plan? Because it will take a lot of time to serve 100+ users with 10 second delay for each.

Thanks a lot for the support!

if you have a static directory set up, then that'd be the place to put your json progress files yes.

re: the 10s delay, that was just an example, you could have a much shorter delay, eg 1s (I would still have some kind of time.sleep in there though, if you just have a busy loop you will chew up your cpu quota very quickly)

Thanks a lot for the support Harry! Happy to know that there is a good alternative for everything on pythonanywhere!