Chapterبەش 3 · Lectureوانە 3.1
Synchronous and Asynchronous Workکاری هاوکات و ناهاوکات
Why an app must not freeze: synchronous work waits at every step, asynchronous work keeps the app responsive while it waits. With a tea-house analogy, the paper's file example told correctly, the 28-second and 10-second timeline, and when that speed-up is real.
بۆچی نابێت گیر بخوات: کاری لە هەر هەنگاوێکدا چاوەڕێ دەکات، کاری لە کاتی چاوەڕوانیدا دەهێڵێتەوە. لەگەڵ بەراوردکارییەکی چایخانە، نموونەی فایلەکەی نووسینەکە بە دروستی، هێڵی کاتی ٢٨ چرکە و ١٠ چرکە و ئەوەی کەی ئەو خێراییە ڕاستە.
What you will learnچی فێر دەبیت
- Explain synchronous (blocking) and asynchronous (non-blocking) work, with an everyday example.
- Explain why a frozen screen is a problem: frames every ~16 ms and Android's 5-second limit.
- Work out the total time of tasks done synchronously and asynchronously, and say when the shorter total is true.
- Tell waiting work from computing work, and choose asynchronous code or a background worker.
- کاری (ڕێگر) و (ناڕێگر) ڕوون بکەیتەوە، لەگەڵ نموونەیەکی ڕۆژانە.
- ڕوونی بکەیتەوە بۆچی شاشەی گیرخواردوو کێشەیە: وێنەیەک هەر نزیکەی ١٦ میلیچرکە و سنووری ٥ چرکەی Android.
- کۆی کاتی ئەرکەکان بە شێوەی و بدۆزیتەوە و بڵێیت کۆی کورتتر کەی ڕاستە.
- کاری چاوەڕوانی لە کاری ژماردن جیا بکەیتەوە و کۆدی یان کرێکارێکی پشتەوە هەڵبژێریت.
Apps spend a lot of time waitingئەپەکان کاتێکی زۆر چاوەڕێ دەکەن
Every app waits. It waits for a to answer, for a to open, for a to find records, for a photo to upload. Over a mobile network one answer can take a fraction of a second or several seconds.
هەموو چاوەڕێ دەکات. چاوەڕێی وەڵامی دەکات، چاوەڕێی کردنەوەی ، چاوەڕێی ئەوەی تۆمارەکان بدۆزێتەوە، چاوەڕێی وێنەیەک. لە یەک وەڵام دەکرێت بەشێک لە چرکەیەک یان چەند چرکەیەک بخایەنێت.
So the real question is: what does the app do while it waits? It can stand still until the answer comes (synchronous), or it can start the job and keep serving the user (asynchronous).
کەواتە پرسیاری ڕاستەقینە ئەوەیە: لە کاتی چاوەڕوانیدا چی دەکات؟ دەتوانێت بوەستێت تا وەڵامەکە دێت ()، یان کارەکە دەست پێ بکات و بەردەوام خزمەتی بکات ().
The words async and await are used in many programming languages, such as JavaScript, C#, Python, Swift and Dart, to control synchronous and asynchronous operations. Before we can use them we need to understand the two kinds of work. This lesson does that without code; each technology track then shows the real keywords.
وشەکانی async و await لە زۆر بەکار دەهێنرێن، وەک JavaScript، C#، Python، Swift و ، بۆ جڵەوکردنی کارە و . پێش ئەوەی بەکاریان بهێنین، پێویستە لەو دوو جۆرە کارە تێبگەین. ئەم وانەیە بەبێ کۆد ئەمە دەکات؛ پاشان هەر ڕێڕەوێکی تەکنەلۆجی وشە کلیلییە ڕاستەقینەکان پیشان دەدات.
Synchronous: one step at a time, and everything waitsهاوکات: هەنگاو بە هەنگاو و هەموو شت چاوەڕێ دەکات
Happening in step, one after another or on a shared clock. In programs, synchronous work is done one step at a time and each step waits for the one before it, so while it waits for a file or the network the whole app freezes. In digital circuits, a synchronous circuit changes all its on the same clock edge.
ئەوەی بە یەک ڕیتم ڕوو دەدات، یەک لە دوای یەک یان بە کاتژمێرێکی هاوبەش. لە کاری هاوکات هەنگاو بە هەنگاو ئەنجام دەدرێت و هەر هەنگاوێک چاوەڕێی ئەوەی پێش خۆی دەکات، بۆیە کاتێک چاوەڕێی فایلێک یان دەکات هەموو ئەپەکە دەبەستێت. لە سووڕە دیجیتاڵەکاندا، سووڕی هاوکات هەموو لەسەر هەمان لێواری کاتژمێر دەگۆڕێت.
In synchronous work, tasks run one after the other. Each task waits until the one before it has finished. If one task is slow, the whole program stops for a second or more: it freezes, and the user cannot interact with it until the freeze ends and the program carries on as before.
لە کاری ، ئەرکەکان یەک لەدوای یەک جێبەجێ دەکرێن. هەر ئەرکێک چاوەڕێ دەکات تا ئەوەی پێش خۆی تەواو دەبێت. ئەگەر ئەرکێک خاو بێت، هەموو بۆ چرکەیەک یان زیاتر دەوەستێت: ڕوو دەدات و هیچ لەگەڵیدا نابێت تا دەتوێتەوە و وەک پێشوو کاری خۆی دەکات.
- Open the fileکردنەوەی فایلەکە
The disk needs 3 seconds.
دیسکەکە ٣ چرکەی پێ دەچێت.
- Read the contentsخوێندنەوەی ناوەڕۆکەکە
Starts only after step 1 has finished.
تەنها دوای تەواوبوونی هەنگاوی یەکەم دەست پێ دەکات.
- Show the contents on the screenپیشاندانی ناوەڕۆکەکە لەسەر شاشە
Starts only after step 2 has finished.
تەنها دوای تەواوبوونی هەنگاوی دووەم دەست پێ دەکات.
Done synchronously, the whole program waits for each step to finish. During those three steps you cannot scroll and cannot tap a button: the app is slow and unresponsive.
بە شێوەی ، هەموو چاوەڕێی تەواوبوونی هەر هەنگاوێک دەکات. لە کاتی ئەو سێ هەنگاوەدا ناتوانیت بکەیت و ناتوانیت پەنجە بە بنێیت: خاو و .
Synchronous: one step at a time, and everything waitsهاوکات: هەنگاو بە هەنگاو و هەموو شت چاوەڕێ دەکات
Why a frozen screen is a real problemبۆچی شاشەی گیرخواردوو کێشەیەکی ڕاستەقینەیە
A phone redraws its screen many times every second: 60 times on many phones, 90 or 120 times on newer ones. At 60 frames per second each new picture of the screen must be ready in about 16 milliseconds:
مۆبایل لە هەر چرکەیەکدا چەندین جار شاشەکەی دووبارە دەکێشێتەوە: لە زۆر مۆبایلدا ٦٠ جار، لە نوێترەکاندا ٩٠ یان ١٢٠ جار (). بە ٦٠ وێنە لە چرکەیەکدا، هەر وێنەیەکی نوێ دەبێت لە نزیکەی ١٦ میلیچرکەدا ئامادە بێت:
At 90 frames per second the budget is about 11 ms, and at 120 about 8 ms.
بە ٩٠ وێنە لە چرکەیەکدا ماوەکە نزیکەی ١١ میلیچرکەیە و بە ١٢٠ نزیکەی ٨ میلیچرکە.
In almost every app one line of work, the , both draws these frames and answers taps. If it is busy waiting for a file or the network, no new frame is drawn: animations stutter, and taps get no answer.
لە نزیکەی هەموو یەک هێڵی کار، واتە ، هەم ئەم وێنانە دەکێشێت و هەم وەڵامی دەستلێدانەکان دەداتەوە. ئەگەر سەرقاڵی چاوەڕوانی یان بێت، هیچ وێنەیەکی نوێ ناکێشرێت: دەپچڕێن و دەستلێدانەکان وەڵام وەرناگرن.
- A frame that takes longer than its 16 ms is skipped; the user sees stutter, called jank.
- Android calls a frame that takes more than 700 ms a frozen frame.
- If an app does not answer a tap within 5 seconds, Android shows the "App isn't responding" (ANR) dialog, which offers to close the app.
- وێنەیەک کە لە ١٦ میلیچرکە زیاتر بخایەنێت فڕێ دەدرێت؛ پچڕپچڕی دەبینێت کە پێی دەوترێت jank.
- Android بە وێنەیەک کە لە ٧٠٠ میلیچرکە زیاتر بخایەنێت دەڵێت وێنەی گیرخواردوو.
- ئەگەر لە ماوەی ٥ چرکەدا وەڵامی دەستلێدانێک نەداتەوە، Android پەنجەرەی «App isn't responding» (ANR) پیشان دەدات کە پێشنیاری داخستنی دەکات.
Asynchronous: start it, keep going, come back for the resultناهاوکات: دەستی پێ بکە، بەردەوام بە، بگەڕێوە بۆ ئەنجامەکە
Not waiting in step. An asynchronous program starts a slow task, such as a or reading a file, and goes on with other work while it waits, so the app stays . In digital circuits, an asynchronous (ripple) counter lets each trigger the next one instead of using one shared clock.
ئەوەی بە یەک ڕیتم چاوەڕێ ناکات. ناهاوکات ئەرکێکی هێواش دەست پێ دەکات، وەک یان خوێندنەوەی فایلێک و لە کاتی چاوەڕوانیدا بەردەوام دەبێت لە کارەکانی تر، بۆیە ئەپەکە دەمێنێتەوە. لە سووڕە دیجیتاڵەکاندا، ژمێرەری ناهاوکات (ripple) هەر دەهێڵێت فلیپ-فلۆپی دواتر بجووڵێنێت، لە جیاتی کاتژمێرێکی هاوبەش.
The paper describes asynchronous functions as functions that work on their own, separate from the ones before and after them. The point is that the app keeps running while it waits for a function to finish: the user can still interact with it, so it stays responsive.
نووسینەکە وا وەسف دەکات کە سەربەخۆیانە کار دەکەن و پەیوەندییان بە پێشوو و دواترەوە نییە. مەبەستەکە ئەوەیە کە لە کاتی چاوەڕوانیدا بەردەوام دەبێت لە کارکردن تا تەواو دەبێت: هێشتا دەتوانێت لەگەڵیدا هەبێت، بۆیە دەمێنێتەوە.
Describes an app that answers the user at once (taps, scrolling and typing keep working) even while it waits for slow work. An app that freezes is unresponsive.
وەسفی ئەو ئەپەیە کە دەستبەجێ وەڵامی بەکارهێنەر دەداتەوە (دەستلێدان، جووڵاندنی لاپەڕە و نووسین هەر کار دەکەن) تەنانەت لە کاتێکدا چاوەڕێی کارێکی هێواش دەکات. ئەپێک کە دەبەستێت ناوەڵامگۆیە.
Responsive: the app answers at once. Unresponsive: it does not react to taps or scrolling.
: یەکسەر وەڵام دەداتەوە. : وەڵامی دەستلێدان و ناداتەوە.
Put more exactly: the program starts the slow job, does not stand and wait, and goes back to its other work (drawing frames, answering taps). When the result arrives, it continues with the step that needed it.
بە وردی: کارە خاوەکە دەست پێ دەکات، ڕاناوەستێت و چاوەڕێ ناکات؛ دەگەڕێتەوە بۆ کارەکانی تری (کێشانی شاشە، وەڵامدانەوەی دەستلێدان). کاتێک ئەنجامەکە گەیشت، ئەو هەنگاوە بەردەوام دەکات کە پێویستی پێی بوو.
Synchronous code waits for each step; asynchronous code starts a slow task and keeps going, handling the result later.
کۆدی چاوەڕێی هەر هەنگاوێک دەکات؛ کۆدی کارێکی هێواش دەست پێ دەکات و بەردەوام دەبێت، پاشان ئەنجامەکەی وەردەگرێت.
Whole video: synchronous versus asynchronous, shown with a JavaScript timer.
هەموو ڤیدیۆکە: بەرامبەر ، بە (setTimeout) پیشان دەدرێت.
Asynchronous: start it, keep going, come back for the resultناهاوکات: دەستی پێ بکە، بەردەوام بە، بگەڕێوە بۆ ئەنجامەکە
The file example, told correctlyنموونەی فایلەکە، بە دروستی
The paper retells the file example asynchronously: open the file (3 seconds), read the contents ("starts immediately"), show the contents ("starts immediately"); during these three steps you can scroll or tap a button.
نووسینەکە نموونەی فایلەکە بە شێوەی دەگێڕێتەوە: کردنەوەی فایلەکە (٣ چرکە)، خوێندنەوەی ناوەڕۆکەکە («یەکسەر دەست پێ دەکات»)، پیشاندانی ناوەڕۆکەکە («یەکسەر دەست پێ دەکات»)؛ لە کاتی ئەو سێ هەنگاوەدا دەتوانیت بکەیت یان پەنجە بە بنێیت.
- Askداوا بکە
The app asks the system to open and read the file, then goes straight back to handling events.
داوا لە سیستەمەکە دەکات فایلەکە بکاتەوە و بیخوێنێتەوە، پاشان یەکسەر دەگەڕێتەوە بۆ بەڕێوەبردنی .
- Stay responsiveوەڵامگۆ بمێنەوە
For about 3 seconds the user scrolls and taps; the screen keeps changing.
بۆ نزیکەی ٣ چرکە دەکات و پەنجە بە دوگمەکاندا دەنێت؛ شاشەکە بەردەوام دەگۆڕێت.
- Resultئەنجام
The system reports: the contents are ready.
سیستەمەکە ڕادەگەیەنێت: ناوەڕۆکەکە ئامادەیە.
- Showپیشانی بدە
Only now does the app show the contents.
تەنها ئێستا ناوەڕۆکەکە پیشان دەدات.
The tea-house waiterچایچیی چایخانە
A tea house has one waiter. Table 1 orders tea, and the water needs 3 minutes to boil. The synchronous waiter puts the kettle on and stands next to it for 3 minutes; Tables 2 and 3 wave, but he does not move. The asynchronous waiter puts the kettle on, takes Table 2's order, brings Table 3 a glass of water, and comes back when the kettle whistles.
چایخانەیەک یەک چایچیی هەیە. مێزی ١ چا داوا دەکات و ئاوەکە ٣ خولەکی پێویستە تا بکوڵێت. چایچیی کترییەکە دادەنێت و ٣ خولەک لە تەنیشتیدا دەوەستێت؛ مێزی ٢ و ٣ دەست ڕادەوەشێنن، بەڵام ناجووڵێت. چایچیی کترییەکە دادەنێت، داوای مێزی ٢ وەردەگرێت، پەرداخێک ئاو بۆ مێزی ٣ دەبات و کاتێک کترییەکە فیکە دەکات دەگەڕێتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
The kettle boils by itself; that is why the waiter is free. If the work needed the waiter's own hands (cutting a large pile of onions), being asynchronous would not help: you would need a second worker. In an app, waiting for the network or the disk is the kettle; heavy calculation is the onions.
کترییەکە خۆی دەکوڵێت؛ لەبەر ئەوە چایچییەکە ئازادە. ئەگەر کارەکە دەستی خودی چایچییەکەی بویستایە (وردکردنی کۆمەڵێکی گەورە پیاز)، بوون یارمەتی نەدەدا: کرێکارێکی دووەمت پێویست دەبوو. لە ، چاوەڕوانیی یان دیسک کترییەکەیە؛ ژماردنی قورس پیازەکەیە.


| In the tea houseلە چایخانەدا | In an appلە |
|---|---|
| The waiterچایچییەکە | The : it draws the screen and answers the user: شاشەکە دەکێشێت و وەڵامی دەداتەوە |
| The kettle, boiling by itselfکترییەکە، کە خۆی دەکوڵێت | The network, the disk or a server doing the slow part، دیسک یان کە بەشە خاوەکە دەکات |
| Other tables wavingمێزەکانی تر کە دەست ڕادەوەشێنن | Taps, scrolling, animationsدەستلێدان، ، |
| The whistleفیکەکە | The result arriving as a new eventگەیشتنی ئەنجامەکە وەک نوێ |
Four tasks: 28 seconds or 10 seconds?چوار ئەرک: ٢٨ چرکە یان ١٠ چرکە؟
The paper's chart has four tasks: Task 1 takes 10 seconds, Task 2 takes 7, Task 3 takes 5 and Task 4 takes 6. Watch them run both ways on the same time scale:
هێڵکارییەکەی نووسینەکە چوار ئەرکی هەیە: ئەرکی یەک ١٠ چرکە دەخایەنێت، ئەرکی دوو ٧، ئەرکی سێ ٥ و ئەرکی چوار ٦. سەیر بکە چۆن بە هەردوو شێوە لەسەر هەمان پێوەری کات کار دەکەن:


Synchronous: the times add up. Asynchronous: all four waits overlap, so the total is the longest one.
: کاتەکان کۆ دەبنەوە. : هەر چوار چاوەڕوانییەکە پێکەوە دەبن، بۆیە کۆی گشتی هێندەی درێژترینیانە.
In the Flutter track, Waiting for several Futures starts independent waits together in Dart.
لە ڕێڕەوی Flutterـدا، چاوەڕوانیی چەند Futureـێک چاوەڕوانییە سەربەخۆکان لە Dartـدا پێکەوە دەست پێ دەکات.
Four tasks: 28 seconds or 10 seconds?چوار ئەرک: ٢٨ چرکە یان ١٠ چرکە؟
When is the 10-second answer true?کەی وەڵامی ١٠ چرکە ڕاستە؟
The chart is a good picture, but the 10 seconds is true only when the four tasks are independent waits: for example four downloads from four different servers. The waiting happens in the network and on the servers, so the four waits really overlap.
هێڵکارییەکە وێنەیەکی باشە، بەڵام ١٠ چرکەکە تەنها کاتێک ڕاستە کە چوار ئەرکەکە چاوەڕوانیی سەربەخۆ بن: بۆ نموونە چوار لە چوار جیاوازەوە. چاوەڕوانییەکە لە و لەسەر ڕوو دەدات، بۆیە چوار چاوەڕوانییەکە بە ڕاستی پێکەوە دەبن.
- Dependent steps do not overlap. If Task 2 needs Task 1's result, the two together still take seconds.
- Heavy calculation does not overlap on one line of work. If the four tasks are calculations that keep the phone's processor busy, doing them "asynchronously" on the main thread still takes about 28 seconds, and the screen freezes in the meantime.
- هەنگاوی بەستراو پێکەوە نابن. ئەگەر ئەرکی دوو ئەنجامی ئەرکی یەکی پێویست بێت، هەردووکیان پێکەوە هێشتا چرکە دەخایەنن.
- ژماردنی قورس لەسەر یەک هێڵی کار پێکەوە نابێت. ئەگەر چوار ئەرکەکە ژماردن بن کە مۆبایلەکە سەرقاڵ دەکەن، کردنیان بە شێوەی «» لەسەر هێشتا نزیکەی ٢٨ چرکە دەخایەنێت و لەو ماوەیەدا شاشەکە گیر دەخوات.
Waiting is not computingچاوەڕوانی ژماردن نییە
| Waiting work (the kettle)کاری چاوەڕوانی (کترییەکە) | Computing work (the onions)کاری ژماردن (پیازەکە) |
|---|---|
| Asking a server for data over the internetداواکردنی لە لە ڕێگەی | Applying a filter to a large photoدانانی فلتەرێک لەسەر وێنەیەکی گەورە |
| Reading or saving a fileخوێندنەوە یان | Sorting a list of a million itemsڕیزکردنی لیستێکی یەک ملیۆن بڕگەیی |
| A database searchگەڕانێک لە | Compressing a videoبچووککردنەوەی ڤیدیۆیەک |
| A timer, a GPS position, a permission dialogکاتژمێرێک، شوێنێکی ، پەنجەرەیەکی | Encrypting a big file گەورە |
Asynchronous code is the answer to waiting. The slow part happens somewhere else (the network, the disk, a server), so the app only needs to start it and be told when it ends.
کۆدی وەڵامی چاوەڕوانییە. بەشە خاوەکە لە شوێنێکی تر ڕوو دەدات (، دیسک، )، بۆیە تەنها پێویستە دەستی پێ بکات و کاتێک تەواو بوو ئاگادار بکرێتەوە.
Heavy computing needs a second worker. Every platform has one: background threads and coroutines on Android, dispatch queues and Swift tasks on iOS, web workers in the browser, isolates in Dart. The main thread hands over the heavy job and stays free for the user. In the Flutter track, a second isolate does this in Dart.
ژماردنی قورس کرێکارێکی دووەمی پێویستە. هەموو یەکێکی هەیە: threadـی پشتەوە و coroutine لە Android، dispatch queue و taskـی Swift لە iOS، web worker لە ، لە . کارە قورسەکە ڕادەست دەکات و بۆ ئازاد دەمێنێتەوە. لە ڕێڕەوی Flutterـدا، isolateـێکی دووەم ئەمە لە Dartـدا دەکات.
Async handles waiting without blocking, but heavy computing still freezes the screen; that work needs a second worker, an isolate.
کاری بۆ چاوەڕوانییە بەبێ ئەوەی هیچ شتێک ڕابگرێت، بەڵام ژماردنی قورس هێشتا دەبێتە هۆی شاشە؛ ئەو کارە کرێکارێکی دووەمی دەوێت، واتە .
Whole video: async is for waiting, isolates are for heavy computing.
هەموو ڤیدیۆکە: کاری بۆ چاوەڕوانییە و بۆ ژماردنی قورس.
What it looks like in codeلە کۆددا چۆن دەردەکەوێت
Here is the file example in : not a real language, just the steps. First the blocking version:
ئەمە نموونەی فایلەکەیە بە : زمانێکی ڕاستەقینە نییە، تەنها هەنگاوەکانە. سەرەتا وەشانە ڕێگرەکە:
show "Start"
contents ← readFile("notes.txt") // the whole app waits here for 3 s
show contents on the screen
show "End"Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The main thread stops here: no frames, no taps, for 3 seconds.
لێرە دەوەستێت: بۆ ٣ چرکە نە وێنە دەکێشرێت و نە وەڵامی دەستلێدان دەدرێتەوە.
show "Start"
start readFile("notes.txt"), and when it finishes:
show its contents on the screen
show "Still working while the file is read…"
// meanwhile the app keeps drawing, scrolling and answering tapsExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
The reading is started; the code inside runs later, when the contents are ready.
خوێندنەوەکە دەست پێ دەکات؛ کۆدی ناوەوە دواتر کار دەکات، کاتێک ناوەڕۆکەکە ئامادە بوو.
- Lineدێڕی 4
This line runs at once, before the file is ready.
ئەم دێڕە یەکسەر کار دەکات، پێش ئامادەبوونی فایلەکە.
Many languages write the second version with two keywords: async marks a function as asynchronous, and await (usually allowed only inside such a function) waits for a result. Careful: await pauses only that function at that line. The rest of the app, its drawing and its taps, keeps running. In the Flutter track, async and await in Dart shows the real code, and the event loop shows who keeps the app running while a function waits.
زۆر زمان وەشانی دووەم بە دوو دەنووسن: async وەک نیشانە دەکات و await (کە زۆرجار تەنها لەنێو ئەو جۆرە ڕێگەپێدراوە) چاوەڕێی ئەنجامێک دەکات. ئاگاداربە: await تەنها ئەو لەو دێڕەدا ڕادەگرێت. باقیی ، کێشانی شاشە و دەستلێدانەکان، بەردەوام کار دەکەن. لە ڕێڕەوی Flutterـدا، async و await لە Dartـدا کۆدی ڕاستەقینە پیشان دەدات و خولی ڕووداوەکان پیشان دەدات کێ بە کارکردوویی دەهێڵێتەوە کاتێک چاوەڕێ دەکات.
How async and await work: a function returns a Future, await pauses only that function, and the event loop keeps the app running.
چۆن کار دەکەن: دەگەڕێنێتەوە، await تەنها ئەو ڕادەگرێت و لە کارکردندا دەهێڵێتەوە.
Watch 0:53-5:29: an example, async/await, futures, the event loop and await.
لە 0:53 تا 5:29 سەیر بکە: نموونەیەک، ، ، و await.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What is trueڕاستییەکە |
|---|---|
| "Asynchronous means all the steps start at once."« واتە هەموو هەنگاوەکان پێکەوە دەست پێ دەکەن.» | Steps that need each other keep their order. Only the waiting stops blocking the app.ئەو هەنگاوانەی پێویستیان بە یەکترە ڕیزبەندیی خۆیان دەپارێزن. تەنها چاوەڕوانییەکە چیتر ڕێگری لە ناکات. |
| "Asynchronous code makes a heavy calculation faster."«کۆدی ژماردنی قورس خێراتر دەکات.» | It does not. Heavy computing needs another thread or worker.ناکات. ژماردنی قورس thread یان کرێکارێکی تری پێویستە. |
| "Four tasks always finish in the time of the longest."«چوار ئەرک هەمیشە لە کاتی درێژترینیاندا تەواو دەبن.» | Only if they are independent waits.تەنها ئەگەر چاوەڕوانیی سەربەخۆ بن. |
| "Asynchronous is the same as parallel."« هەمان هاوتەریبییە.» | On one line of work the waits overlap (concurrency); true parallel work needs several cores.لەسەر یەک هێڵی کار چاوەڕوانییەکان پێکەوە دەبن (concurrency)؛ کاری هاوتەریبی ڕاستەقینە چەند پێویستە. |
| "A one-second freeze does not matter."« یەک چرکە گرنگ نییە.» | One second is about 60 lost frames; users notice, and after 5 s Android offers to close the app.یەک چرکە نزیکەی ٦٠ وێنەی لەدەستچووە؛ هەستی پێ دەکەن و دوای ٥ چرکە Android پێشنیاری داخستنی دەکات. |
"await stops the whole app."«await هەموو ڕادەگرێت.» | It pauses only its own function; the app keeps drawing and answering.تەنها خۆی ڕادەگرێت؛ بەردەوام دەکێشێت و وەڵام دەداتەوە. |
| Mixing up synchronous with sync (keeping copies of data the same).تێکەڵکردنی لەگەڵ . | Different ideas: synchronous is about waiting; sync copies changes between devices or a server.دوو بیرۆکەی جیاوازن: پەیوەندی بە چاوەڕوانییەوە هەیە؛ گۆڕانکارییەکان لە نێوان ئامێرەکان یان دەگوازێتەوە. |
Exercisesڕاهێنانەکان
Say whether each job is mainly waiting or mainly computing: (a) downloading a PDF; (b) turning a 4K video into a smaller file; (c) asking a server for today's bus times; (d) finding the shortest route through a big city map on the phone; (e) waiting for the user to allow the camera.
بڵێ هەر کارێک زیاتر چاوەڕوانییە یان زیاتر ژماردن: (أ) PDFـێک؛ (ب) گۆڕینی ڤیدیۆیەکی 4K بۆ بچووکتر؛ (ج) داواکردنی کاتەکانی پاسی ئەمڕۆ لە ؛ (د) دۆزینەوەی کورتترین ڕێگا لە نەخشەی شارێکی گەورەدا لەسەر مۆبایلەکە؛ (هـ) چاوەڕوانیی ئەوەی ڕێگە بە کامێرا بدات.
Show answerوەڵامەکە پیشان بدە
- (a) Waiting (network).
- (b) Computing: use a background worker.
- (c) Waiting (server).
- (d) Computing.
- (e) Waiting (the user).
- (أ) چاوەڕوانی ().
- (ب) ژماردن: کرێکارێکی پشتەوە بەکاربهێنە.
- (ج) چاوەڕوانی ().
- (د) ژماردن.
- (هـ) چاوەڕوانی ().
Three tasks take 8 s, 3 s and 5 s. (a) How long synchronously? (b) How long asynchronously, if they are independent downloads? (c) How long if the 5-second task needs the result of the 8-second task, and the 3-second task is independent?
سێ ئەرک ٨، ٣ و ٥ چرکە دەخایەنن. (أ) بە شێوەی چەندی دەوێت؟ (ب) بە شێوەی چەندی دەوێت، ئەگەر سەربەخۆ بن؟ (ج) چەندی دەوێت ئەگەر ئەرکی ٥ چرکەیی پێویستی بە ئەنجامی ئەرکی ٨ چرکەیی هەبێت و ئەرکی ٣ چرکەیی سەربەخۆ بێت؟
Hintئاماژە
Independent waits overlap; dependent ones add up.
چاوەڕوانیی سەربەخۆ پێکەوە دەبن؛ بەستراوەکان کۆ دەبنەوە.
Show answerوەڵامەکە پیشان بدە
In (c) the 8-second task and then the 5-second task take 13 s; the 3-second download happens at the same time.
لە (ج)دا ئەرکی ٨ چرکەیی و دوای ئەویش ئەرکی ٥ چرکەیی پێکەوە ١٣ چرکە دەخایەنن؛ ٣ چرکەیی لە هەمان کاتدا ڕوو دەدات.
Exercisesڕاهێنانەکان
More exercisesڕاهێنانی زیاتر
A social app does this after you log in: (1) check the password with the server, (2) load your profile, (3) load your profile picture, (4) load your messages. Steps 2–4 need step 1; steps 3 and 4 need the user id from step 2. Which steps can wait at the same time?
کۆمەڵایەتی دوای ئەمانە دەکات: (١) پشکنینی وشەی نهێنی لەگەڵ ، (٢) هێنانی پرۆفایلەکەت، (٣) هێنانی وێنەی پرۆفایلەکەت، (٤) هێنانی نامەکانت. هەنگاوەکانی ٢ تا ٤ پێویستیان بە هەنگاوی ١ـە؛ هەنگاوەکانی ٣ و ٤ ناسنامەی لە هەنگاوی ٢ پێویستە. کام هەنگاوانە دەتوانن لە هەمان کاتدا چاوەڕێ بکەن؟
Show answerوەڵامەکە پیشان بدە
Only steps 3 and 4: after step 2 gives the user id, the picture and the messages can be loaded at the same time. Steps 1 → 2 must stay in order. The whole time, the app should stay responsive and show a loading sign.
تەنها هەنگاوەکانی ٣ و ٤: دوای ئەوەی هەنگاوی ٢ ناسنامەی دەدات، وێنەکە و نامەکان دەتوانن لە هەمان کاتدا بهێنرێن. هەنگاوەکانی ١ ← ٢ دەبێت بە ڕیز بمێننەوە. لە هەموو ئەو ماوەیەدا دەبێت بمێنێتەوە و نیشانەیەکی چاوەڕوانی پیشان بدات.
In your own words, explain the paper's file example (open 3 s, read, show) twice: once synchronously and once asynchronously. Say what the user can do in each case, and what stays the same.
بە وشەی خۆت نموونەی فایلەکەی نووسینەکە (کردنەوە ٣ چرکە، خوێندنەوە، پیشاندان) دوو جار ڕوون بکەرەوە: جارێک بە شێوەی و جارێک بە شێوەی . بڵێ لە هەر حاڵەتێکدا دەتوانێت چی بکات و چی وەک خۆی دەمێنێتەوە.
Show answerوەڵامەکە پیشان بدە
Synchronous: the app opens, reads and shows the file, waiting at each step; for about 3 seconds the user cannot scroll or tap. Asynchronous: the app starts the opening and reading and keeps serving the user; when the contents are ready it shows them. In both cases the order open → read → show stays the same, and the contents appear after about 3 seconds; only the frozen screen disappears.
: فایلەکە دەکاتەوە، دەیخوێنێتەوە و پیشانی دەدات و لە هەر هەنگاوێکدا چاوەڕێ دەکات؛ بۆ نزیکەی ٣ چرکە ناتوانێت بکات یان پەنجە بە دوگمەیەکدا بنێت. : کردنەوە و خوێندنەوەکە دەست پێ دەکات و بەردەوام خزمەتی دەکات؛ کاتێک ناوەڕۆکەکە ئامادە بوو پیشانی دەدات. لە هەردوو حاڵەتدا ڕیزبەندیی کردنەوە ← خوێندنەوە ← پیشاندان وەک خۆی دەمێنێتەوە و ناوەڕۆکەکە دوای نزیکەی ٣ چرکە دەردەکەوێت؛ تەنها شاشە گیرخواردووەکە نامێنێت.
Summaryپوختە
- Synchronous work runs one step after another, and the program waits at each step. A slow step freezes the app: no scrolling, no taps.
- Asynchronous work starts the slow job and keeps the app responsive; the result is handled when it arrives.
- Steps that need each other keep their order. "Everything starts at once" is wrong.
- Independent waits overlap: s becomes s. Dependent steps and heavy calculation do not overlap that way.
- The main thread draws a frame every ~16 ms and answers taps; keep waiting off it with asynchronous code and heavy computing off it with another worker.
- کاری هەنگاو لەدوای هەنگاو کار دەکات و لە هەر هەنگاوێکدا چاوەڕێ دەکات. هەنگاوێکی خاو گیر دەخات: نە و نە دەستلێدان.
- کاری کارە خاوەکە دەست پێ دەکات و دەهێڵێتەوە؛ ئەنجامەکە کاتێک دەگات بەڕێوە دەبرێت.
- ئەو هەنگاوانەی پێویستیان بە یەکترە ڕیزبەندیی خۆیان دەپارێزن. «هەموو شتێک پێکەوە دەست پێ دەکات» هەڵەیە.
- چاوەڕوانیی سەربەخۆ پێکەوە دەبن: چرکە دەبێتە چرکە. هەنگاوی بەستراو و ژماردنی قورس بەو شێوەیە پێکەوە نابن.
- هەر نزیکەی ١٦ میلیچرکە جارێک وێنەیەک دەکێشێت و وەڵامی دەستلێدان دەداتەوە؛ چاوەڕوانی بە کۆدی و ژماردنی قورس بە کرێکارێکی تر لێی دوور بخەرەوە.
Why does a frozen app feel broken even if it finishes later?بۆچی ئەپێکی گیرخواردوو وەک تێکچوو هەست دەکرێت تەنانەت ئەگەر دواتر تەواو بێت؟
Because no frames are drawn and no taps are answered; after 5 seconds Android even offers to close it.
چونکە هیچ وێنەیەک ناکێشرێت و وەڵامی هیچ دەستلێدانێک نادرێتەوە؛ دوای ٥ چرکە Android تەنانەت پێشنیاری داخستنی دەکات.
In the tea-house analogy, what is the limit?سنووری بەراوردکاریی چایخانەکە چییە؟
The kettle boils by itself. If the work needs the waiter's own hands (heavy computing), asynchronous alone does not help; you need another worker.
کترییەکە خۆی دەکوڵێت. ئەگەر کارەکە دەستی خودی چایچییەکەی بوێت (ژماردنی قورس)، بە تەنها یارمەتی نادات؛ کرێکارێکی ترت پێویستە.
What does await pause?await چی ڕادەگرێت؟
Only the function it is written in; the rest of the app keeps running.
تەنها ئەو تێیدا نووسراوە؛ باقیی بەردەوام کار دەکات.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
URL, URI and URNURL، URI و URN
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.