Posts

Showing posts with the label asynchronous

Qt async call: how to run something after an async call has finished its job

Image
Clash Royale CLAN TAG #URR8PPP Qt async call: how to run something after an async call has finished its job I have code like below which does an async call: QMetaObject::invokeMethod(this, "endSelectionHandling", Qt::QueuedConnection); I want to modify the code like this: QMetaObject::invokeMethod(this, "endSelectionHandling", Qt::QueuedConnection); // I want to add statements here which depend on the result of the above async call. // How can I wait for the above async call to finish its jobs? How can I wait for Qt asycn call to finish its job? Is there a better approach? If you want to wait right after the function call then why don't you call it directly i.e. blocking call? That would be the perfect solution in your case unless there's something in between that you want to do. – Azeem yesterday ...

Fast-csv read several files synchronously

Image
Clash Royale CLAN TAG #URR8PPP Fast-csv read several files synchronously I'm trying to read several files synchronously with fast-csv, it should looks like: read file 1 execute something while reading read file 2 execute something while reading (it must be execute after first execution's file that's why I need to do this synchronously) ... Here is my code simplified: const csv = require('fast-csv'); const PROCEDURES = [ { "name": "p1", "file": "p1.csv" }, { "name": "p2", "file": "p2.csv" }, ]; const launchProcedure = (name, file) => { try { const fs = require("fs"); const stream = fs.createReadStream(file, { encoding: 'utf8' }); console.log('launching parsing...'); stream.once('readable', () => { // ignore first line let chunk; while (null !== (chunk = stream....

Asynchronously upload images to Cloudinary using the Python API

Image
Clash Royale CLAN TAG #URR8PPP Asynchronously upload images to Cloudinary using the Python API I am trying to asynchronously upload images to Cloudinary using their Python API. Their documentation states the following is required to upload an image. result = cloudinary.uploader.upload(file, **options) Since, I would like to upload asynchronously, it appears I need to set the "async" option to True (also in the documentation). async (Boolean): Tells Cloudinary whether to perform the upload request in the background (asynchronously). Default: false. Since options has **, as explained in this SO post, I assume that the function accepts keyword arguments like so. response = await cloudinary.uploader.upload(img, async=True) However, when I run my script, I get the following error: File "async_upload.py", line 16 response = await cloudinary.uploader.upload(img, async=True) ^ SyntaxError: invalid syntax How d...

Mocha tests are ignored when used within repetitive stream callbacks

Image
Clash Royale CLAN TAG #URR8PPP Mocha tests are ignored when used within repetitive stream callbacks My task is to test some js function against big test vector corpus (10M+), also requirement to test in browser forced me to stream testvectors from local webserver, rather than synchronously read from file record by record. So, I'm obtaining (one per line ascii) test vectors this way: const http = require('http'); const readline = require('readline'); FetchTestVectors = function(filename, recordCb) { const url = "http://localhost:8080/" + filename; http.get(url) .on('response', function(response) { readline.createInterface({ input: response }) .on('line', function(line) { //recordCb(line.split(':')); recordCb(line); }) }); } And when trying to test it, inner describe-it block becomes ignored (all callbacks). However, I can see...

HttpPost does not get ContentLength from PostAsync request

Image
Clash Royale CLAN TAG #URR8PPP HttpPost does not get ContentLength from PostAsync request I have a unit test code that sends request to an HttpPost method. [Test] public async Task ValidateToken() { var content = new FormUrlEncodedContent(new { new KeyValuePair<string, string("test", "test") }; httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var response = await httpClient.PostAsync("/api/token/1", content); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); } [Route("api/token")] public class MyController : BaseApiController { private readonly HttpRequest httpRequest; public MyController(IHttpContextAccessor httpContextAccessor) { httpRequest = httpContextAccessor.HttpContext.Request; } [HttpPost("{count}")] public async Task<IActionResult> Post(int count) { if...

How do I return the response from an asynchronous call?

Image
How do I return the response from an asynchronous call? I have a function foo which makes an Ajax request. How can I return the response from foo ? foo foo I tried returning the value from the success callback as well as assigning the response to a local variable inside the function and returning that one, but none of those ways actually return the response. success function foo() { var result; $.ajax({ url: '...', success: function(response) { result = response; // return response; // <- I tried that one as well } }); return result; } var result = foo(); // It always ends up being `undefined`. @MaximShoustin I've added a JavaScript only answer (no jQuery) but like Felix said - "How do I return the response from an AJAX call" is really a nice way to say "How does concurrency work in JavaScript". Not to mention Angular's $http is very similar to jQuery...