Issue40005
Created on 2020-03-18 19:50 by Bharatsolanki, last changed 2020-03-20 20:44 by tim.peters. This issue is now closed.
Files | ||||
---|---|---|---|---|
File name | Uploaded | Description | Edit | |
test.py | Bharatsolanki, 2020-03-18 20:39 |
Messages (8) | |||
---|---|---|---|
msg364559 - (view) | Author: Bharat Solanki (Bharatsolanki) | Date: 2020-03-18 19:50 | |
Hi Team, Below code is giving different result in python 2.7 and 3.7 version. Code is running fine when i am using 2.7 but in 3.7, it is showing error. from multiprocessing import Pool import traceback class Utils: def __init__(self): self.count = 10 def function(): global u1 u1 = Utils() l1 = range(3) process_pool = Pool(1) try: process_pool.map(add, l1, 1) process_pool.close() process_pool.join() except Exception as e: process_pool.terminate() process_pool.join() print(traceback.format_exc()) print(e) def add(num): total = num + u1.count print(total) if __name__ == "__main__": function() Could you please help me on this how can it run in 3.7 version. Thanks, Bharat |
|||
msg364560 - (view) | Author: Eric V. Smith (eric.smith) * ![]() |
Date: 2020-03-18 19:59 | |
Note that 2.7 is no longer supported. If you think you found a bug in 3.7, then please: - Reformat your code so that we can understand it. - Show us what output you actually get. - Show us what output you expect, and why. |
|||
msg364562 - (view) | Author: Bharat Solanki (Bharatsolanki) | Date: 2020-03-18 20:39 | |
When you run this test.py in 2.7. you will get output (10 11 20), But in 3.7, Its getting failed. Its showing below error: "Traceback (most recent call last): File "test.py", line 14, in function process_pool.map(add, l1, 1) File "C:\Users\Bharat Solanki\Anaconda3\lib\multiprocessing\pool.py", line 268, in map return self._map_async(func, iterable, mapstar, chunksize).get() File "C:\Users\Bharat Solanki\Anaconda3\lib\multiprocessing\pool.py", line 657, in get raise self._value NameError: name 'u1' is not defined" Please let me know if you need any other information. Thanks Bharat |
|||
msg364563 - (view) | Author: Bharat Solanki (Bharatsolanki) | Date: 2020-03-18 20:42 | |
u1 is global variable. So I can use it any where. Rigth! Thanks Bharat On Thu, Mar 19, 2020, 2:09 AM Bharat Solanki <report@bugs.python.org> wrote: > > Bharat Solanki <bharatslnk02@gmail.com> added the comment: > > When you run this test.py in 2.7. you will get output (10 11 20), But in > 3.7, Its getting failed. Its showing below error: > > "Traceback (most recent call last): > File "test.py", line 14, in function > process_pool.map(add, l1, 1) > File "C:\Users\Bharat Solanki\Anaconda3\lib\multiprocessing\pool.py", > line 268, in map > return self._map_async(func, iterable, mapstar, chunksize).get() > File "C:\Users\Bharat Solanki\Anaconda3\lib\multiprocessing\pool.py", > line 657, in get > raise self._value > NameError: name 'u1' is not defined" > > Please let me know if you need any other information. > > Thanks > Bharat > > ---------- > Added file: https://bugs.python.org/file48979/test.py > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue40005> > _______________________________________ > |
|||
msg364564 - (view) | Author: Tim Peters (tim.peters) * ![]() |
Date: 2020-03-18 20:55 | |
u1 is a global _only_ in a process that runs `function()`, which declares u1 global and assigns it a value. You have no sane expectation that a worker process (none of which run `function()`) will know anything about it. Are you sure you're running 2.7 and 3.7 on the same machine? It's impossible that this code ever "worked" under Windows, but it might under Linux-y systems, which use `fork()` to create worker processes. The traceback you showed was obviously run under Windows. Under Python 2.7.11 on Windows, I get the same kind of error: NameError: global name 'u1' is not defined This is the code: from multiprocessing import Pool import traceback class Utils: def __init__(self): self.count = 10 def function(): global u1 u1 = Utils() l1 = range(3) process_pool = Pool(1) try: process_pool.map(add, l1, 1) process_pool.close() process_pool.join() except Exception as e: process_pool.terminate() process_pool.join() print(traceback.format_exc()) print(e) def add(num): total = num + u1.count print(total) if __name__ == "__main__": function() |
|||
msg364596 - (view) | Author: Bharat Solanki (Bharatsolanki) | Date: 2020-03-19 06:51 | |
Hi Tim, Thank you for clearing that up. I ran the same code in 2.7 and 3.7 on Windows, Its showing the same error. Yes, Its running in Linux-y systems. It depends on OS. Regards, Bharat On Thu, Mar 19, 2020 at 2:25 AM Tim Peters <report@bugs.python.org> wrote: > > Tim Peters <tim@python.org> added the comment: > > u1 is a global _only_ in a process that runs `function()`, which declares > u1 global and assigns it a value. You have no sane expectation that a > worker process (none of which run `function()`) will know anything about it. > > Are you sure you're running 2.7 and 3.7 on the same machine? It's > impossible that this code ever "worked" under Windows, but it might under > Linux-y systems, which use `fork()` to create worker processes. > > The traceback you showed was obviously run under Windows. Under Python > 2.7.11 on Windows, I get the same kind of error: > > NameError: global name 'u1' is not defined > > This is the code: > > from multiprocessing import Pool > import traceback > > class Utils: > def __init__(self): > self.count = 10 > > def function(): > global u1 > u1 = Utils() > l1 = range(3) > process_pool = Pool(1) > try: > process_pool.map(add, l1, 1) > process_pool.close() > process_pool.join() > except Exception as e: > process_pool.terminate() > process_pool.join() > print(traceback.format_exc()) > print(e) > > def add(num): > total = num + u1.count > print(total) > > if __name__ == "__main__": > function() > > ---------- > nosy: +tim.peters > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue40005> > _______________________________________ > |
|||
msg364717 - (view) | Author: Terry J. Reedy (terry.reedy) * ![]() |
Date: 2020-03-20 20:38 | |
Bharat, when responding by email, *please* delete from your response the the post you are responding to. Your response is added below that message, and including it is duplicate noise. Tim, you seem to be saying that this should be closed as 'not a bug'. Correct? |
|||
msg364718 - (view) | Author: Eric V. Smith (eric.smith) * ![]() |
Date: 2020-03-20 20:42 | |
I agree it's not a bug. It's a known difference between Windows and Linux, due to fork() semantics. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2020-03-20 20:44:15 | tim.peters | set | status: open -> closed resolution: not a bug stage: resolved |
2020-03-20 20:42:24 | eric.smith | set | messages: + msg364718 |
2020-03-20 20:38:56 | terry.reedy | set | versions:
+ Python 3.7, - Python 2.7 nosy: + terry.reedy messages: + msg364717 type: compile error -> behavior |
2020-03-19 06:51:53 | Bharatsolanki | set | messages: + msg364596 |
2020-03-18 20:55:38 | tim.peters | set | nosy:
+ tim.peters messages: + msg364564 |
2020-03-18 20:42:31 | Bharatsolanki | set | messages: + msg364563 |
2020-03-18 20:39:29 | Bharatsolanki | set | files:
+ test.py messages: + msg364562 |
2020-03-18 19:59:15 | eric.smith | set | nosy:
+ eric.smith messages: + msg364560 |
2020-03-18 19:50:43 | Bharatsolanki | create |