دالة parallelize
شغّل تكرارات الحلقة عبر الخيوط
دالة parallelize درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Core Is Not Enough
A plain loop runs on a single core while the rest sit idle. For heavy work, that wastes most of your CPU.
for i in range(n):
out[i] = heavy(i)Spread the Work
Parallelism means handing different loop iterations to different cores so they all work at the same time. 🧵
Meet parallelize
Mojo's parallelize helper runs your loop body across many threads for you, so you skip the manual thread plumbing.
from algorithm import parallelizeThe Work Closure
You write a small function that does the work for one index. Mojo calls it many times, once per iteration.
fn work(i: Int):
out[i] = heavy(i)Pass the Closure as a Parameter
The closure goes in square brackets as a compile-time parameter, not as a normal call argument. Mojo specializes on it.
parallelize[work](n)The Count Is Runtime
The number in the parentheses is how many work items to run. That count is an ordinary runtime value.
parallelize[work](num_items)Ask the Machine for Cores
Use num_physical_cores from the sys module to learn how many real cores you have to spread work across.
from sys import num_physical_coresCap the Worker Count
An optional second argument sets how many workers run at once. Matching it to your cores often works best.
parallelize[work](n, num_physical_cores())Each Call Is Independent
parallelize assumes work items do not depend on each other's order. Each call should stand on its own.
Best for Heavy Loops
Parallelism shines when each item does real work. For tiny loops, the thread overhead can cost more than it saves.
Measure the Speedup
Time the loop before and after going parallel. A real benchmark tells you whether the extra threads actually helped.
Quick Check
You want to run a loop body across several CPU cores in Mojo.
Recap
You import parallelize, pass a per-item closure as a bracket parameter and the count in parentheses, then size workers to your cores for a real speedup. 🚀
الأسئلة الشائعة
هل درس «دالة parallelize» مجاني؟
نعم — نص درس «دالة parallelize» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «دالة parallelize»؟
شغّل تكرارات الحلقة عبر الخيوط تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «دالة parallelize»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- دالة parallelize
- تقسيم العمل إلى أجزاء
- دمج التوازي والمتجهات
- تجنب سباقات البيانات