פייתון/פייתון גרסה 2/תכנות מקבילי
מראה
< פייתון | פייתון גרסה 2
הרצת פקודה ברקע התוכנית
[עריכה]|
שקלו לדלג על נושא זה סעיף זה עוסק ב־ |
כידוע, תוכנית מחשב מכילה פקודות לביצוע סדרתי, והפקודה הבאה לא תבוצע לפני תום הפקודה הנוכחית. אך כאשר מריצים תוכנות חיצוניות (באמצעות 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'))
קישורים חיצוניים
[עריכה]
| - | תכנות מקבילי | - |