donko1
@donko1
Изучаю python

В чем разница между этими программами?

Почему этот код работает
import multiprocessing
import time

def worker():
	name = multiprocessing.current_process().name
	print(name, 'Starting')
	time.sleep(2)
	print(name, 'Exiting')


def my_service():
	name = multiprocessing.current_process().name
	print(name, 'Starting')
	time.sleep(3)
	print(name, 'Exiting')


if __name__ == '__main__':
	service = multiprocessing.Process(
	name='my_service',
	target=my_service,
	)
	worker_l = multiprocessing.Process(
	name='worker 1',
	target=worker,
	)
	worker_2 = multiprocessing.Process( 
	target=worker,
	)

	service.start()
	worker_l.start()
	worker_2.start()

А этот нет:
from time import sleep
import multiprocessing

def worker():
	name = multiprocessing.current_process().name
	print(name, ' - start')
	sleep(2)
	print(name, ' - end')


def service():
	name = multiprocessing.current_process().name
	print(name, 'Starting')
	sleep(3)
	print(name, 'Exiting')


if __name__ == '__main__':
	service = multiprocessing.Process(
	name='my_service',
	target=service,
	)
	worker_l = multiprocessing.Process(
	name='worker 1',
	target=worker,
	)
	worker_2 = multiprocessing.Process( # default name
	target=worker,
	)

	service.start()
	worker_l.start()
	worker_2.start()

Второй выдает:
Traceback (most recent call last):
  File "C:\Users\Name\OneDrive\Рабочий стол\test.py", line 31, in <module>
    service.start()
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function service at 0x00000204086E4A60>: it's not the same object as __main__.service
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
  File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 87, in steal_handle
    _winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
PermissionError: [WinError 5] Отказано в доступе
  • Вопрос задан
  • 109 просмотров
Пригласить эксперта
Ответы на вопрос 1
@galaxy
За именами переменных следите
def service(): # <---------------
  name = multiprocessing.current_process().name
...
if __name__ == '__main__':
  service = multiprocessing.Process( <---------------
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы