ത്രെഡുകൾ#
ആമുഖം#
ത്രെഡുകൾ ഒരു റോബോട്ടിനെ ഒരേ പ്രോഗ്രാമിൽ ഒരേസമയം ഒന്നിലധികം ജോലികൾ ചെയ്യാൻ അനുവദിക്കുന്നു. അവ മൾട്ടിടാസ്കിംഗ് പ്രാപ്തമാക്കുന്നു, ഇത് റോബോട്ടിനെ ഒരേ സമയം സ്വതന്ത്ര പ്രവർത്തനങ്ങൾ നടത്താൻ അനുവദിക്കുന്നു.
കുറിപ്പ്: ഒരു ത്രെഡിനൊപ്പം ഉപയോഗിക്കുന്നതിന് നിങ്ങൾ ആദ്യം ഒരു ഫംഗ്ഷൻ നിർവചിക്കണം.
കൺസ്ട്രക്ടർ – പുതിയ ത്രെഡുകൾ സൃഷ്ടിക്കുകയും ആരംഭിക്കുകയും ചെയ്യുക.
Thread– പ്രധാന പ്രോഗ്രാമിനൊപ്പം സമാന്തരമായി നിർദ്ദിഷ്ട ഫംഗ്ഷൻ പ്രവർത്തിപ്പിക്കുന്ന ഒരു പുതിയ ത്രെഡ് ആരംഭിക്കുന്നു.
പ്രവർത്തനം – റണ്ണിംഗ് ത്രെഡുകൾ നിയന്ത്രിക്കുക.
stop– പശ്ചാത്തല സ്വഭാവം നിർത്താൻ ഉപയോഗപ്രദമായ ഒരു ത്രെഡ് സ്വമേധയാ നിർത്തുന്നു.
കൺസ്ട്രക്ടർ#
Thread#
Thread ഒരു ത്രെഡ് സൃഷ്ടിക്കുകയും ആരംഭിക്കുകയും ചെയ്യുന്നു. നിങ്ങൾ ഒരു ത്രെഡ് സൃഷ്ടിക്കുമ്പോൾ, അത് ആവശ്യമില്ലാത്തപ്പോൾ നിർത്താൻ നിങ്ങൾക്ക് അതിന് പേര് നൽകാം.
ഉപയോഗം:
my_thread = Thread(my_function)
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
|
ഓപ്ഷണൽ. പുതിയ ത്രെഡിന് ഒരു പേര്. |
|
The name of a previously defined function. |
Note: A function must always be defined before it is called.
# Drive forward while blinking lights
def blink_lights():
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, BLUE)
wait(0.5, SECONDS)
blink_lights_thread = Thread(blink_lights)
robot.move_at(0)
# Run multiple threads simultaneously
# Turn right, blink lights and display heading at once
def blink_lights():
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
def display_heading():
while True:
robot.screen.clear_row()
robot.screen.set_cursor(1, 1)
robot.screen.print(f"Heading: {robot.inertial.get_yaw()}")
wait(50, MSEC)
blink_lights_thread = Thread(blink_lights)
display_heading_thread = Thread(display_heading)
robot.turn(RIGHT)
ആക്ഷൻ#
stop#
stop ഒരു ത്രെഡ് സ്വമേധയാ നിർത്തുന്നു, ഒരു ടാസ്ക് ഇനി ആവശ്യമില്ലാത്തപ്പോഴോ ഒരു പ്രോഗ്രാമിന് ത്രെഡുകൾ പുനഃസജ്ജമാക്കേണ്ടിവരുമ്പോഴോ ഇത് ഉപയോഗപ്രദമാണ്. ഒരു ത്രെഡ് നിർത്തിയാൽ, അത് പുനരാരംഭിക്കാൻ കഴിയില്ല. അത് വീണ്ടും പ്രവർത്തിപ്പിക്കുന്നതിന്, നിങ്ങൾ ത്രെഡ് ഉപയോഗിച്ച് ഒരു പുതിയ ത്രെഡ് സൃഷ്ടിക്കണം.
ഉപയോഗം:
my_thread.stop()
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
|
മുമ്പ് ആരംഭിച്ച ഒരു ത്രെഡ് ഒബ്ജക്റ്റ്. |
"""
Run multiple threads simultaneously
Turn right, blink lights and display heading at once
Stop blinking lights after 2 seconds
"""
def blink_lights():
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.off(ALL_LEDS)
wait(0.5, SECONDS)
def display_heading():
while True:
robot.screen.clear_row()
robot.screen.set_cursor(1, 1)
robot.screen.print(f"Heading: {robot.inertial.get_yaw()}")
wait(50, MSEC)
blink_lights_thread = Thread(blink_lights)
display_heading_thread = Thread(display_heading)
robot.turn(RIGHT)
wait(2, SECONDS)
blink_lights_thread.stop()