как писать поддерживаемый код?
Most common mistake software developers make: putting stuff in the wrong place. Coupling responsibilities and concepts that should be kept separate.
For me, this is 95% of software development. Just figuring out *where* things belong.
Что гуглить, что учить?
Может литературу какую почитать посоветуете?
Можно ли себя называть миддлом, если твой код говно?
Как перестать говнокодить и принимать неверные архитектурные решения?
которое высылается каждый день в pdf файле в виде таблицы
from threading import *
def work(i):
for _ in range(100):
print(f"hello i'm a thread #{i}")
t1 = Thread(target=work, args=(1,))
t2 = Thread(target=work, args=(2,))
t1.start()
t2.start()
t1.join()
t2.join()
from threading import *
lock = Lock()
def work(i):
for _ in range(100):
with lock:
print(f"hello i'm a thread #{i}")
t1 = Thread(target=work, args=(1,))
t2 = Thread(target=work, args=(2,))
t1.start()
t2.start()
t1.join()
t2.join()
from threading import *
from time import sleep
class GlobalState:
def __init__(self, x):
self.x = x
def set_x(self, x):
self.x = x
def reader(state: GlobalState):
if state.x % 2 == 0:
sleep(0.01) # simulate OS context switch
print(f"{state.x=} is even")
else:
print(f"{state.x=} is odd")
def changer(state: GlobalState):
state.set_x(state.x + 1)
state = GlobalState(2)
t1 = Thread(target=reader, args=(state,))
t2 = Thread(target=changer, args=(state,))
t1.start()
t2.start()
t1.join()
t2.join()
from threading import *
from time import sleep
class GlobalState:
def __init__(self, x):
self.x = x
self.lock = Lock()
def set_x(self, x):
self.x = x
def reader(state: GlobalState):
with state.lock:
if state.x % 2 == 0:
sleep(0.01) # simulate OS context switch
print(f"{state.x=} is even")
else:
print(f"{state.x=} is odd")
def changer(state: GlobalState):
with state.lock:
state.set_x(state.x + 1)
state = GlobalState(2)
t1 = Thread(target=reader, args=(state,))
t2 = Thread(target=changer, args=(state,))
t1.start()
t2.start()
t1.join()
t2.join()
from threading import *
from random import *
class GlobalState:
def __init__(self):
self.x = []
def do_something_changing(self):
if random() < 0.5:
self.x.append(1)
elif self.x:
self.x.pop()
def reader(state: GlobalState):
for _ in range(10000000):
if len(state.x) % 2 == 0:
if len(state.x) % 2 != 0: # wtf how it's possible?
print(f"length {len(state.x)} was even before context switch")
def changer(state: GlobalState):
for _ in range(10000000):
state.do_something_changing()
state = GlobalState()
t1 = Thread(target=reader, args=(state,))
t2 = Thread(target=changer, args=(state,))
t1.start()
t2.start()
t1.join()
t2.join()