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

Numpy can't be used in embedded player 2x in row without restarting #1105

Closed
BluePrintRandom opened this issue Jan 28, 2020 · 18 comments
Closed

Comments

@BluePrintRandom
Copy link
Member

https://www.youtube.com/watch?v=aNqfsnGQKRQ

if we import numpy, close engine -> start again -> import numpy we get some error messages

nummpy.zip

@paul-marechal
Copy link
Collaborator

Does using module mode help, rather than script mode?

@BluePrintRandom
Copy link
Member Author

BluePrintRandom commented Jan 29, 2020

image
nope*

@paul-marechal
Copy link
Collaborator

paul-marechal commented Jan 29, 2020

Seems to be a bug with numpy itself.

The BGE makes a backup of the modules before running the embedded player, and restores it at the end. This ensures that modules are not cached for eternity (until you restart Blender itself), and allows your game to always load modules from disk. Else it would always hit the cache and never update modules.

The embedded player does not have this issue since no backup and restore is required.

The approximate call chain leading to the backup of the modules:

file: /source/gameengine/Ketsji/KX_PythonInit.cpp

- initGamePlayerPythonScripting
  - initPySysObjects
    - backupPySysObjects

I guess you can wrap numpy's importation somehow, since the sys object is not entirely backup'ed:

# file: numpy_wrapper.py

import sys

if 'numpy' not in sys.modules and hasattr(sys, '__numpy__'):
    # restore module cache with backup ref
    numpy = sys.modules['numpy'] = sys.__numpy__

else:
    import numpy
    # store a backup ref
    sys.__numpy__ = numpy

And then to use it:

from numpy_wrapper import numpy

@youle31
Copy link
Collaborator

youle31 commented Apr 30, 2020

I can't reproduce a bug with numpy in last master with this test file:

https://pasteall.org/blend/c18e144cf3194a829ccdc7cce2851f56

(neither in embedded, neither in standalone)

@BluePrintRandom
Copy link
Member Author

BluePrintRandom commented Apr 30, 2020 via email

@BluePrintRandom
Copy link
Member Author

image
this is from this build hash
image

it fails the second time I run your test file @youle31

@BluePrintRandom
Copy link
Member Author

BluePrintRandom commented Apr 30, 2020

(the first time it made kinda a rainbow on the cube?)
and in standalone
image

@youle31
Copy link
Collaborator

youle31 commented Apr 30, 2020

I can't reproduce this bug (even if I restart 50 times, I have no error message). Yes, it is an example from gpu module doc

@lordloki
Copy link
Member

I can confirm it.
image
It seems that is an issue of numpy itself (numpy/numpy#14384)
A mitigation as pointer by an user in the numpy github tracker is to downgrading numpy to numpy==1.15.4
I'm going to see if it is possible this solution

@lordloki
Copy link
Member

lordloki commented Apr 30, 2020

It works!! but then we have a new issue with the version 1.15.4. We have the following error
image
and then we don't have audio. We can not solve this because _multiarray_umath needs numpy 1.16 and the numpy reload needs version 1.15.4

We will have to live with this issue.

@BluePrintRandom
Copy link
Member Author

Ok - since we can use it in standalone it's not that big of a deal,

maybe we can think of a fix long term and come back to this?

I will close for now,

@paul-marechal
Copy link
Collaborator

The issue comes from the fact that numpy does monkey patching on module initialization, and the BGE re-initialize numpy in a global Python context where the monkey patch cannot be applied twice. You can find the workaround code here:

# file: numpy_wrapper.py

import sys

if 'numpy' not in sys.modules and hasattr(sys, '__numpy__'):
    # restore module cache with backup ref
    numpy = sys.modules['numpy'] = sys.__numpy__

else:
    import numpy
    # store a backup ref
    sys.__numpy__ = numpy

And then to use it:

from numpy_wrapper import numpy

@lordloki
Copy link
Member

Thanks. I will include in the release notes.

@paul-marechal
Copy link
Collaborator

paul-marechal commented May 1, 2020

Just tried to import numpy on 0.2.5 and I don't get the crash... But when it fails with the ... already has a docstring my workaround should apply.

@BluePrintRandom
Copy link
Member Author

BluePrintRandom commented May 1, 2020 via email

@BluePrintRandom
Copy link
Member Author

Is there a way to use your workaround if in embedded automatically
?

@youle31
Copy link
Collaborator

youle31 commented May 1, 2020

it's weird that you have this error and not me, maybe it is because of the way we build blender...

EDIT: I have with_PYTHON_INSTALL_NUMPY enabled by default in my cmake. I build with cmake 3.15.3

@lordloki
Copy link
Member

lordloki commented May 1, 2020

I also have WITH_PYTHON_INSTALL_NUMPY enabled and my cmake is 3.15.3.
Very weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants