Last active
December 28, 2022 22:16
-
-
Save Alexsandr0x/bd7b86b51fd4faccff23793249a6d893 to your computer and use it in GitHub Desktop.
Basico de Paralelização em Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
class Dog(): | |
def __init__(self, name): | |
self.name = name | |
self.amount_of_food = 0 | |
self.food_necessary = 10 | |
def eat(self, pot): | |
""" | |
Faz doguinho comer | |
retorna true se ele comeu, retorna false caso ele já esteja saciado | |
""" | |
if self.amount_of_food == self.food_necessary: | |
print(f"Dog {self.name} Is already satified!") | |
return False | |
if len(pot) > 0: | |
pot.pop() | |
self.amount_of_food += 1 | |
print(f"Dog {self.name} ate 1 snack hungry level = {self.amount_of_food/self.food_necessary}") | |
return True | |
else: | |
print(f"The pot is empty!") | |
return False | |
def check_hungry(self): | |
ratio = self.amount_of_food/self.food_necessary | |
if ratio >= 0.5: | |
print(f"Dog {self.name} is {ratio * 100}% fed! Well Done!") | |
else: | |
print(f"Dog {self.name} is {ratio * 100}% fed! he will die 😭😭😭") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pot = ['food' for i in range(10)] | |
dog1 = Dog("Pipoca") | |
dog2 = Dog("Rosbife") | |
eated = True | |
while eated: | |
time.sleep(0.25) | |
eated = dog1.eat(pot) | |
dog1.check_hungry() | |
eated = True | |
while eated: | |
time.sleep(0.25) | |
eated = dog2.eat(pot) | |
dog2.check_hungry() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pot = ['food' for i in range(10)] | |
dog1 = Dog("Pipoca") | |
eated = True | |
while eated: | |
time.sleep(0.25) | |
eated = dog1.eat(pot) | |
dog1.check_hungry() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
pot = ['food' for i in range(10)] | |
dog1 = Dog("Pipoca") | |
dog2 = Dog("Rosbife") | |
def task(dog, pot): | |
eated = True | |
while eated: | |
time.sleep(0.25) | |
eated = dog.eat(pot) | |
t1 = Thread(target=task, args=(dog1, pot)) | |
t2 = Thread(target=task, args=(dog2, pot)) | |
t1.start() | |
t2.start() | |
t1.join() | |
t2.join() | |
dog1.check_hungry() | |
dog2.check_hungry() | |
t2.start() | |
t1.join() | |
t2.join() | |
dog1.check_hungry() | |
dog2.check_hungry() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment