Appearance
question:Exception has occurred: AttributeError module 'multiprocessing.shared_memory' has no attribute 'RawValue' File "C:meuscripta.py", line 102, in main numeroA = shared_memory.RawValue('i', 0, True) File "C:meuscripta.py", line 137, in <module> main() AttributeError: module 'multiprocessing.shared_memory' has no attribute 'RawValue'
answer:I apologize for the confusion. You are correct that the `multiprocessing.shared_memory` module does not have a `RawValue` function. Instead, we can use `multiprocessing.Value` to create a shared variable. Here's the modified code: ```python import time from ctypes import sizeof, c_int, c_char from multiprocessing import shared_memory, Process, Value def main(): int_shm = shared_memory.SharedMemory(name='numeroA', create=True, size=sizeof(c_int)) numeroA = Value('i', 0, lock=True) str_shm = shared_memory.SharedMemory(name='nomeA', create=True, size=sizeof(c_char) * 13) nomeA = Value('c', b'nome original', lock=True) def scriptB(numeroA, nomeA): while True: print('—scriptB—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(3) processB = Process(target=scriptB, args=(numeroA, nomeA)) processB.start() while True: with numeroA.get_lock(): numeroA.value += 1 with nomeA.get_lock(): nomeA.value = b"novo nome" print('—scriptA—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(5) int_shm.close() str_shm.close() if __name__ == "__main__": main() ``` In the modified code, I replaced `shared_memory.RawValue` with `multiprocessing.Value`. The `lock=True` parameter is added to ensure thread-safe access to the shared variables. I also used `numeroA.get_lock()` and `nomeA.get_lock()` to acquire locks before modifying the shared variables in the main process. This helps in preventing race conditions when accessing the shared variables concurrently. This should fix the `AttributeError` and allow you to use shared variables in your code.
question:Exception has occurred: TypeError one character bytes, bytearray or integer expected File "C:meuscripta.py", line 108, in main nomeA = Value('c', b'nome original', lock=True) File "C:meuscripta.py", line 138, in <module> main() TypeError: one character bytes, bytearray or integer expected
answer:I apologize for the mistake. The `Value` function from the `multiprocessing` module only supports a single character for the type when using the `'c'` type code. In order to store a string of more than one character in shared memory, we need to use `multiprocessing.Array` instead. Here's the modified code: ```python import time from ctypes import sizeof, c_int, c_char from multiprocessing import shared_memory, Process, Value, Array def main(): int_shm = shared_memory.SharedMemory(name='numeroA', create=True, size=sizeof(c_int)) numeroA = Value('i', 0, lock=True) str_shm = shared_memory.SharedMemory(name='nomeA', create=True, size=sizeof(c_char) * 13) nomeA = Array(c_char, b'nome original', lock=True) def scriptB(numeroA, nomeA): while True: print('—scriptB—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(3) processB = Process(target=scriptB, args=(numeroA, nomeA)) processB.start() while True: with numeroA.get_lock(): numeroA.value += 1 with nomeA.get_lock(): nomeA.value = b"novo nome" print('—scriptA—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(5) int_shm.close() str_shm.close() if __name__ == "__main__": main() ``` In the modified code, I used `multiprocessing.Array` to create the shared memory for the string variable `nomeA`. The type code `c_char` is used to specify that each element in the array is a single character. The initial value for the array is given as `b'nome original'`. Now, the code should run without the `TypeError` and allow you to use shared variables for integer and string values.
question:Exception has occurred: AttributeError Can't pickle local object 'main.<locals>.scriptB' File "C:meuscripta.py", line 121, in main processB.start() File "C:meuscripta.py", line 138, in <module> main() AttributeError: Can't pickle local object 'main.<locals>.scriptB'
answer:I apologize for the mistake. The `AttributeError` occurs because the `scriptB` function cannot be pickled, which is necessary for it to be used as a target function for a new process. To resolve this issue, you can define the `scriptB` function outside the `main()` function, making it a top-level function. This way, it can be pickled and used as the target function for the new process. Here's the modified code: ```python import time from ctypes import sizeof, c_int, c_char from multiprocessing import shared_memory, Process, Value, Array def scriptB(numeroA, nomeA): while True: print('—scriptB—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(3) def main(): int_shm = shared_memory.SharedMemory(name='numeroA', create=True, size=sizeof(c_int)) numeroA = Value('i', 0, lock=True) str_shm = shared_memory.SharedMemory(name='nomeA', create=True, size=sizeof(c_char) * 13) nomeA = Array(c_char, b'nome original', lock=True) processB = Process(target=scriptB, args=(numeroA, nomeA)) processB.start() while True: with numeroA.get_lock(): numeroA.value += 1 with nomeA.get_lock(): nomeA.value = b"novo nome" print('—scriptA—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(5) int_shm.close() str_shm.close() if __name__ == "__main__": main() ``` In the modified code, the `scriptB` function is defined outside the `main()` function scope. This allows it to be pickled and used as the target function for the new process. With this modification, the code should run without the `AttributeError` and both the `scriptA` and `scriptB` processes should be able to communicate through the shared variables.
question:Estou confuso. Exponha-me as coisas como deve ser. tenho #scriptA.py: import time from ctypes import sizeof, c_int, c_char from multiprocessing import shared_memory, Process, Value, Array # Define a função que será executada pelo processo filho def scriptB(numeroA, nomeA): while True: print('—scriptB—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(3) def main(): # Cria uma memória compartilhada para o inteiro e lê o valor inicial dele int_shm = shared_memory.SharedMemory(name='numeroA', create=True, size=sizeof(c_int)) numeroA = Value('i', 0, lock=True) #numeroA = shared_memory.RawValue('i', 0, True) #numeroA = shared_memory.RawValue('i', numeroA.value) # Cria uma memória compartilhada para a string e lê o valor inicial dela str_shm = shared_memory.SharedMemory(name='nomeA', create=True, size=sizeof(c_char) * 13) nomeA = Array(c_char, b'nome original', lock=True) #nomeA = shared_memory.RawArray('c', nomeA) # Cria o processo filho processB = Process(target=scriptB, args=(numeroA, nomeA)) processB.start() while True: # Modifica os valores das variáveis compartilhadas with numeroA.get_lock(): numeroA.value += 1 with nomeA.get_lock(): nomeA.value = b"novo nome" print('—scriptA—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(5) # Libera a memória compartilhada int_shm.close() str_shm.close() if __name__ == "__main__": main() este script ainda é necessário.: #scriptB.py: import time from multiprocessing import shared_memory def main(): # Cria uma memória compartilhada para o inteiro e escreve um valor inicial nele int_shm = shared_memory.SharedMemory(name='numeroA') numeroA = shared_memory.RawValue('i', 0) numeroA = shared_memory.RawValue('i', numeroA.value) # Cria uma memória compartilhada para a string e escreve um valor inicial nela str_shm = shared_memory.SharedMemory(name='nomeA') nomeA = shared_memory.RawArray('c', b'nome original') nomeA = shared_memory.RawArray('c', nomeA) while True: print("—scriptB—") print(f"numeroA: {numeroA.value}") print(f"nomeA: {nomeA.value.decode()}") time.sleep(3) # Libera a memória compartilhada int_shm.close() str_shm.close() if __name__ == "__main__": main()
answer:Claro, vou simplificar as coisas para você. Aqui está o código revisado: No arquivo `scriptA.py`: ```python import time from ctypes import sizeof, c_int, c_char from multiprocessing import shared_memory, Process, Value, Array # Define a função que será executada pelo processo filho def scriptB(numeroA, nomeA): while True: print('—scriptB—') print(f'numeroA: {numeroA.value}') print(f'nomeA: {nomeA.value.decode()}') time.sleep(3) def main(): # Cria uma memória compartilhada para o inteiro e lê o valor inicial dele int_shm = shared_memory.SharedMemory(name='numeroA', create=True, size=sizeof(c_int)) numeroA = Value('i', 0, lock=True) # Cria uma memória compartilhada para a string e lê o valor inicial dela str_shm = shared_memory.SharedMemory(name='nomeA', create=True, size=sizeof(c_char) * 13) nomeA = Array(c_char, b'nome original', lock=True) # Cria o processo filho processB = Process(target=scriptB, args=(numeroA, nomeA)) processB.start() while True: # Modifica os valores