Why the modules in the pic below are Overlapping? - asynchronous

Let's suppose we are doing this
require(['module1', 'module2', 'module3'], function(m1, m2, m3){
//Doing something after they load
});
What will happen exactly?
I feel this will happen: First the require will construct the filepaths for these modules by using baseUrl or path and then she wil send get request for these modules and will ask O/s kernels to load these files and when done ??? (I guess there is no event loop here)
I am totally confused please help me What will happen ?

In the first graphic the modules are load synchronous, which means module1 is requested and if it was loaded successfully module2 starts loading and so on.
In the second graphic the modules overlapping cause the requests going out after each other but all files are loading asynchronous, which means there is no waiting for one module to get loaded successfully before starting the next request.
So requireJs load all the script asynchronous and waits for all request to get loaded and after this it injects all modules into your function.

Related

NextJS - Read Data Once From File and usable in any module

I am trying to implement the following design. Read data from a file (xml) at server startup and have it available as in memory variables to be used in the backend api for certain calculations. This data never changes thus it only need to be read once.
I am getting alot of module not found errors as I believe from what I read is that FS functions should only be done on the server side using getStaticProps
But this will trigger the read request every time a client loads the page.
Can someone guide me with a simple example on how to do this so that the data is read once and usable in the back end server side modules for calculations
Thanks

How can I load an i18n json from a CMS with i18next?

I am using https://github.com/i18next/next-i18next and rather than hardcoding the files, I want to use HygraphCMS (formerly GraphCMS) to provide those strings. What's the best way to approach this?
If I wait for an async request, it'll slow things down. Thanks in advance!
I pumped into similar issue before, what I did was I created a script that runs before the dev and build commands, something like:
// ...
"scripts": {
// ....
"trans": "node ./scripts/get-i18n.js",
"dev":"npm run trans && next dev",
"build":"npm run trans && next build"
}
// ...
And you write a script get-i18n.js to fetch you translations from the CMS and save them in the directory you chosed in the i18n setup.
The downside of this approach that if the translation changed in the CMS you would need to restart the server everytime, or run the script manually in another shell to make the fetch and update the strings.
The best solution here would depend on how you have the rest of the Next.js project set up and how much you want to avoid an async request. I've described two approaches I would consider below. Both examples assume you're using Next.js and hosting on Vercel, but it should be possible and similar through other platforms.
1. Build script to store locally (without async request)
Start by writing a script to fetch all the translations and store them locally in the project (just as Aldabil21 said).
After that, you can create a deploy webhook and call it from your CMS whenever a change is made; this will ensure that the translations are always kept up-to-date. An issue with this could be that the build runs too often, so you may want to add some conditions here to prevent that, such as only calling the webhook from the CMS when the translations content changes.
2. Using incremental static regeneration (with async request)
Of course, if you're using incremental static regeneration, you might reconsider fetching the translations using getStaticProps as the request is not made for each visitor.
The result of the request to the CMS's translation collection would be cached on Vercel's Edge network and then shared amongst each visitor, so only the first request after the cache has expired will trigger the full request. The maximum age for the caching of static files is 31 days, so this delay when requesting fresh data could be infrequent enough to be acceptable. Note that you have to enable this manually by setting the revalidate prop in the return object of getStaticProps.
You could even mitigate this request further (depending on your project setup) by querying only for the language that is currently used, and querying a new language client-side only when it is requested by the visitor (or possibly once the page is idle or the language switcher is opened). If you have many languages, this reduction will reduce the download time substantially.
If you do go the getStaticProps route and are using Next.js >12.2.0 then you could also create a CMS webhook to call the on-demand revalidation endpoint whenever a page is updated, which will cause the fresh translations to be stored in cache before a user gets a chance to request it, removing the delay for all users. Or you could use the webhook in the same way as mentioned in 1 and trigger a new build (with the new translations) every time the translation collection is updated.

Serve web request in python that spawns a new long running subprocess

I currently have a python command line application that uses python invoke package to organise, list and execute tasks. There are many task files (controlled & created by users, not me). Execution time for some task files can be more than an hour. Each task is actually a test script/program. invoke is useful in listing/executing all the tasks in a task file (we call it a testsuite) or only a bunch of them (a tasks collection) or a single task. (Having a ton of loose scripts and organising, listing & running them in the way users want would be quite a task, hence invoke).
However, invoke cannot be used as a library. It does not offer an API that can be leveraged to list and run test tasks. So I am forced to run invoke as a shell command in subprocess from command line program. I replace (via execl()) the current process with invoke because once the control passes to invoke, there is no need to come back to parent process. So far good..
Now, there is a requirement that this command line program be callable from a web application. So I need to wrap this cmdline program in a restful http API. I've decided to use bottle.py to keep things simple.
I understand that the long running testsuite (tasks) will have to be done off the http request/response cycle. But I'm unable to finalise exactly how to go about it (prob. I may be overthiniking). But here is what I want ...
Tasks are written by users. They are always synchronous, they may sleep or execute shell commands via subprocess.run().
Application is internal, it will not be bombarded with huge number of requests. No of users Max. 10.
But each request (of type that runs the task) will take minutes and some cases > hour to complete. New requests during this should not block.
Calling application (running on a different host) will need to report progress of the running task to the browser UI. ('progress bar')
Ability to communicate with running task and 'cancel' it from browser UI.
With above situation, am I correct in saying ..
because a new 'process' must be spawnned (due use of subprocess and excl in current code) for a request, it rules out using 'threads' of any type (os threads, greenlets, gevent)?
Using any async libraries (web framework, web/http server or in app code) won't be of much help, because every run request will have to be a new process anyway?
How will the process be spawned when a request comes in? Let the web/htpp server (gunicorn?) do it? or My application has to take case of forking itself?
is 'gunicorn' a good choice for this situation?
I have a feeling that users may also ask for the ability to schedule tasks/tests. I might end up using some sort of task queue. I have read 'huey' and feel that it is light & simple for my needs. (No redis/Celery). But any task queue also means a separate consumer process to administer? More moving parts to the mix.
'progress-bar' functionality means, subprocess has to keep updating its progress somewhere and calling application has to read from there. Does this necessitate 'task queue' anyway?
There is a lot of material on all of this and I have read quite some if it. But it still has left me unclear as to how exactly to go about implementing my requirements. Any direction/pointers would be appreciated. I'd also appreciate any advice on what 'not to use'.
If you need something really simple then you could write a wrapper around task spooler (linux tool to run tasks) https://vicerveza.homeunix.net/~viric/soft/ts/ (especially https://vicerveza.homeunix.net/~viric/soft/ts/article_linux_com.html for more details)
Otherwise it's probably better to switch to uwsgi spooler, rq with redis or celery with rabbitmq (cause with redis it works to certain extent).

Updating Apple Watch Complication by fetching data using a URL request

I am having a complication that needs to update once in a while by fetching data from a server.
I am trying to fetch the data from the Watch. I am doing so by scheduling a WKRefreshBackgroundTask. When this task fires, I start a URL session to fetch some JSON data from a server.
After the data has been fetched successfully, I complete the WKApplicationRefreshBackgroundTask by calling its setTaskCompletedWithSnapshot(false).
This all seems to work OK-ish, however, I'd rather create a WKURLSessionRefreshBackgroundTask after the WKApplicationRefreshBackgroundTask to download it in the background and consume less time in the background tasks. I think this is not possible, I am not downloading or uploading files. If I understand correctly WKURLSessionRefreshBackgroundTask with a URLSessionConfiguration.background(withIdentifier:) can only be used a for URLSessionUploadTasks or URLSessionDownloadTasks, so I had to forget about that idea. (Please correct me if I misunderstood that).
Now the biggest problem I run into is that when the Watch is locked (taken off the wrist, put on the charging puck, etc.) the URL Requests are not being executed. And thus the data is not been updated. How should we deal with this? The activity compilation simply turns dark when the Watch gets locked and as soon as you unlock the Watch it reloads. Is there anything we can do to understand the Watch is locked/unlocked?
Is it even the right approach to fetch the data from the Watch using a WKApplicationRefreshBackgroundTask, or is it better to wake up the phone and get the phone to request the data using WatchConnectivity? And if so: which WCSessionDelegate functions would you use to achieve that?
If fetching the data directly from the Watch is the way to go: can I set the WKApplicationRefreshBackgroundTask to completed before the request has completed, or should I wait until after the request completed? (i.e., at what point in time should WKApplicationRefreshBackgroundTask.setTaskCompletedWithSnapshot() be called).
I read loads and loads of articles and watched multiple videos. Yet still I find it very hard to find what the correct architecture is to fetch compilation data from a server and get this working rock solid.
Hopefully someone can shine a little light on this matter.

Load Data via WebRequst on StartUp

I want to load data from the web, when my application starts, and
actually wait for the data, because the data will affect the Apps Appearance.
I am using WindowsPhone and c#.
The Problem I have is, that all WebRequest.BeginGetResponse type methods are Asynchronous,
but when I block my main Thread to wait for a result, the "asynchronous" method seems to be blocked to.
Is there an easy way to wait for the result?
Maybe an API that allows better control at Downloading files from the web?
Based on your response in the comment above, I would recommend the following:
Show your splashscreen if you have one, then a loading screen.
Don't block the UI thread, for three reasons:
You don't need to
You need the UI thread to keep going so you can show indeterminate progress or some other type of loading animation
Windows Phone will kill your app if the UI thread is locked for a certain amount of time
After you've loaded your loading screen, start your BeginGetResponse.
After EndGetResponse is completed, do what you need to and then navigate to the main page of the application.
Good luck!

Resources