פייתון/תכנות מקבילי
מתוך ויקיספר, אוסף ספרי הלימוד והמדריכים החופשי.
< פייתון
| חלק זה של הספר הינו קצרמר. אתם מוזמנים לתרום לוויקיספר ולערוך אותו. |
תוכן עניינים |
[עריכה] הרצת פקודה ברקע התוכנית
|
שקול לדלג על נושא זה סעיף זה עוסק ב- |
כידוע, תוכנית מחשב מכילה פקודות לביצוע סדרתי, והפקודה הבאה לא תבוצע לפני תום הפקודה הנוכחית. אך כאשר מריצים תוכנות חיצוניות (באמצעות os.system) המצב שונה, מכיוון שאפשר לבקש ממערכת ההפעלה לטפל במקביליות. בשורת הפקודה דבר זה נעשה על ידי הפקודה start בחלונות והאופרטור & ביוניקס/לינוקס.
אם לדוגמה ברצונכם להפעיל את דפדפן פיירפוקס, תוך המשכת התוכנית שלכם, ניתן לעשות זאת באמצעות:
#!/usr/bin/env python import sys import os app='firefox' if sys.platform.startswith('win'): command='start ' + app elif os.name=='posix ': command=app + ' &' os.system(command) # rest of your code...
[עריכה] הרצת פונקציות מקביליות
import thread def loop_and_print(msg): for i in range(1,10): print msg thread.start_new_thread(loop_and_print, ('hello')) thread.start_new_thread(loop_and_print, ('hello'))
import thread def loop_and_print(msg, num): for i in range(1,num): print msg thread.start_new_thread(loop_and_print, ('hello', 1000))
[עריכה] נעילות
import thread import threading l = threading.Lock() def loop_and_print(msg): for i in range(1,10000): l.acquire() print msg l.release() thread.start_new_thread(loop_and_print, ('hello')) thread.start_new_thread(loop_and_print, ('world'))
import thread import threading l = threading.Lock() def loop_and_print(msg): l.acquire() for i in range(1,10000): print msg l.release() thread.start_new_thread(loop_and_print, ('hello')) thread.start_new_thread(loop_and_print, ('world'))
[עריכה] קישורים חיצוניים
| הפרק הקודם: הרחבות בעזרת שפות נמוכות יותר |
תכנות מקבילי | הפרק הבא: נספחים |

