我有一个具有这种层次结构的程序:
SRC
├───Game
├───Main
│ └───main.py
└───Video
└───video_input.py
我正在尝试从以下位置运行此代码main.py
:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'D:/vscode workspace/cv project/src/Video')))
from video_input import takeInput
NP0 = np.zeros((3,), dtype= np.uint8)
lock = Lock()
def printMovement(name : str):
print(colored(f'{name}: from game', 'blue'))
existing_shm = shared_memory.SharedMemory(name= name)
movement = np.ndarray((3,), dtype= np.uint8, buffer= existing_shm.buf)
print(f'movement = {movement}, type = {type(movement[0])}')
while True:
if movement.all() != NP0.all():
print(movement)
if keyboard.is_pressed('q'):
print(f'exiting')
break
existing_shm.close()
def create_shared_block(name: str):
a = NP0 # Start with an existing NumPy array
shm = shared_memory.SharedMemory(create=True, size=a.nbytes, name= name)
# # Now create a NumPy array backed by shared memory
np_array = np.ndarray(a.shape, dtype=np.uint8, buffer=shm.buf)
np_array[:] = a[:] # Copy the original data into shqqared memory
return shm, np_array
if current_process().name == "MainProcess":
shr, movement = create_shared_block('sportometer movement block')
print(colored(f'{shr.name}: from main', 'yellow'))
with ProcessPoolExecutor(max_workers=2) as executor:
executor.submit(takeInput, shr.name)
executor.submit(printMovement, shr.name)
shr.close()
shr.unlink()
并且takeInput
从中video_input.py
更改movement
共享内存变量的函数。以相同的方式takeInput
访问movement
共享内存变量printMovement
:
print(colored(f'{name}: from video input', 'green'))
existing_shm = shared_memory.SharedMemory(name= name)
# movement acts like a pointer to the Object`s buffer?
movement = np.ndarray((3,), dtype= np.uint8, buffer= existing_shm.buf)
while 'open':
# to make printMovement print 100% but even after this line it Doesnt
movement[:] = NP0[0] + 1
# changing movement here to something else (but not to NP0)
这两个函数都有一个while True
循环。
我正在尝试movement
按名称访问共享内存块,但它失败了。即使进程内部发生了变化,该printMovement
函数也不打印任何内容。movement
video_input.py