Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module functions and variables not found #185

Open
dnsmkl opened this issue Jun 22, 2023 · 5 comments
Open

module functions and variables not found #185

dnsmkl opened this issue Jun 22, 2023 · 5 comments
Labels

Comments

@dnsmkl
Copy link

dnsmkl commented Jun 22, 2023

Parallel stream raises NameError for anything defined at module level.

System/Version:

  • pyfunctional==1.4.3
  • windows 10 (same problem does not occur on linux)

Minimal reproducing code:

    import functional
    
    def myprint(arg):
        print(arg)

    if __name__ == "__main__":
        pseq = functional.streams.ParallelStream(processes=1)
        pseq(1,2,3).map(myprint).all() # works
        pseq(4,5,6).map(lambda x: myprint(x)).all() # fails - NameError: name 'myprint' is not defined

Expected output:

  1
  2
  3
  4
  5
  6

Actual output:

  1
  2
  3
  multiprocessing.pool.RemoteTraceback: 
  """
  Traceback (most recent call last):
    File "c:\Program Files\Python311\Lib\multiprocessing\pool.py", line 125, in worker
      result = (True, func(*args, **kwds))
                      ^^^^^^^^^^^^^^^^^^^
    File "C:\reproduce\venv\Lib\site-packages\functional\util.py", line 121, in unpack
      return list(result)
             ^^^^^^^^^^^^
    File "C:\reproduce\app\main.py", line 9, in <lambda>
      pseq(4,5,6).map(lambda x: myprint(x)).all() # fails - NameError: name 'myprint' is not defined
                                ^^^^^^^
  NameError: name 'myprint' is not defined
  """
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File "C:\reproduce\app\main.py", line 9, in <module>
      pseq(4,5,6).map(lambda x: myprint(x)).all() # fails - NameError: name 'myprint' is not defined
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\reproduce\venv\Lib\site-packages\functional\pipeline.py", line 675, in all
      return all(self)
             ^^^^^^^^^
    File "C:\reproduce\venv\Lib\site-packages\functional\util.py", line 167, in lazy_parallelize
      for pool_result in pool.imap(unpack, packed_partitions):
    File "c:\Program Files\Python311\Lib\multiprocessing\pool.py", line 873, in next
      raise value
  NameError: name 'myprint' is not defined
  
  Process finished with exit code 1

It also fails when referencing module level import, or variable, or function that uses other function:

    import functional
    import os
    def printarg(arg): print(arg)
    def chain_f(arg): printarg(arg)
    MYCONST = 1
    def printconst(arg): print(MYCONST)
    def printosname(arg): print(os.name)
    if __name__ == "__main__":
        pseq = functional.streams.ParallelStream()
        pseq(1).map(printarg).all() # works
        # and the rest fail:
        pseq(2).map(printconst).all() # NameError: name 'MYCONST' is not defined
        pseq(3).map(lambda x: print(MYCONST)).all() # NameError: name 'MYCONST' is not defined
        pseq(4).map(chain_f).all() # NameError: name 'printarg' is not defined
        pseq(5).map(lambda x: printarg(x)).all() # NameError: name 'printarg' is not defined
        pseq(6).map(printosname).all() # NameError: name 'os' is not defined
        pseq(7).map(lambda x: print(os.name)).all() # NameError: name 'os' is not defined
@artemisart
Copy link
Contributor

This could be an issue/particularities of python multiprocessing on windows, not pyfunctional. Can you try wrapping your code in a def main function and calling from the if __main__ ?

@Yangeok
Copy link

Yangeok commented Jun 28, 2023

Is pseq working in ipykernel file?

@stale
Copy link

stale bot commented Aug 12, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 12, 2023
@EntilZha
Copy link
Owner

I'd agree with @artemisart, this seems like a namespace issue with parallel computation. Could you check if using the multiprocessing modules results in something similar? In general, a good idea to have a main method anyway since otherwise things in the __main__ block are module scope.

@Yangeok not sure what you mean.

@stale stale bot removed the stale label Aug 14, 2023
@stale
Copy link

stale bot commented Oct 15, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants