# attempt at a health system?
class Enemy:
def __init__(self,name,health,damage):
self.name = name
self.health = health
self.damage = damage
def attack(self):
print(f"The {goblin.name} attacks! Causing {goblin.damage} damage!")
player.health -= Enemy.damage
print(f"You have {player.health}")
goblin = Enemy("goblin",100,20)
class Player:
def __init__(self,health,damage):
self.health = health
self.damage = damage
player = Player(200,40)
goblin.attack()
error:
Traceback (most recent call last):File "c:\Users\user\Desktop\python stuff\project.py", line 10, in attackplayer.health -= Enemy.damage^^^^^^^^^^^^AttributeError: type object 'Enemy' has no attribute 'damage'
import numpy as np
from multiprocessing import shared_memory
from mpi4py import MPI
comm = MPI.COMM_WORLD
pid = comm.Get_rank()
npros = comm.Get_size()
npts = 10
dtyp = np.int64
if pid == 0:
arr = np.arange(npts, dtype=dtyp)
shm = shared_memory.SharedMemory(create=True, size=arr.nbytes, name='trial')
brr = np.ndarray(arr.shape, dtype=arr.dtype, buffer=shm.buf)
brr[:] = arr[:]
del arr
comm.barrier()
if pid == 1:
existing_shm = shared_memory.SharedMemory(name='trial')
crr = np.ndarray((npts), dtype=dtyp, buffer=existing_shm.buf)
print(crr)
comm.barrier()
if pid == 1:
del crr
existing_shm.close()
comm.barrier()
if pid == 0:
del brr
shm.close()
shm.unlink()
但是,它会返回一些警告
/home/peedaruos/miniconda/envs/py39/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked shared_memory objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '
/home/peedaruos/miniconda/envs/py39/lib/python3.9/multiprocessing/resource_tracker.py:229: UserWarning: resource_tracker: '/trial': [Errno 2] No such file or directory: '/trial' warnings.warn('resource_tracker: %r: %s' % (name, e))