from ctypes import c_wchar_p
from random import choice, randint
from time import sleep
from multiprocessing import Value, Process
class Values(object):
def __init__(self):
self._text = Value(c_wchar_p, None)
@property
def text(self):
return self._text.value
def add_text(self, text: str):
if self._text.value:
self._text.value += text
else:
self._text.value = text
def clear_text(self):
self._text.value = ""
class A(Process):
def __init__(self, storage: Values):
Process.__init__(self)
self.storage = storage
def run(self) -> None:
while True:
self.storage.add_text(choice(["a", "b", "c"]))
print(self.storage.text)
sleep(1)
class B(Process):
def __init__(self, storage: Values):
Process.__init__(self)
self.storage = storage
def run(self) -> None:
while True:
a = randint(0, 10)
if a > 5:
pass
else:
self.storage.clear_text()
print(self.storage.text)
sleep(0.001)
if __name__ == '__main__':
s = Values()
a = A(s)
b = B(s)
a.start()
b.start()