Reputation: 3938
I am executing locally a python script through a php exec function and everything works fine. Now its the time I need to move the project in the server and I am facing some problem when it comes in executing the same python script.
In both cases I have the same version of Python (2.7.3) and all the required libs are installed. I have spotted where the issue is created but I can not figure out why. Its this line in my python script:
import networkx as nx
CG1_nodes=nx.connected_components(G1)[:]
It runs successfully in the locally but it crashes in the server. I have found out that if I remove the:
[:]
then it works. I have also checked the content of G1 and its populated. Any idea what I am missing here?
Upvotes: 1
Views: 96
Reputation: 23887
You should check if you have the same version of networkx in both cases.
In older networkx versions nx.connected_components(G1)
was a list. In the newer version (1.9.1) it's a generator. If X
is a generator, then X[:]
doesn't work. But if X
is a list it does. So if your machine and the server have different versions, then in one case it would be allowable but not the other.
You "fixed" this by removing the [:]
, and so CG1_nodes
is now a generator rather than a list. As long as your earlier use of it is consistent with a generator, the results would (probably) be the same. So the two codes would work. Obviously making it a list explicitly would work, but might be memory intensive.
More details are documented here. In particular note:
To recover the earlier behavior, use
list(connected_components(G))
.
I believe the previous version returned the list sorted by decreasing component size. The new version is not sorted. If you need it sorted you'll need to do a bit more:
sorted(list(nx.connected_components(G)), key = len, reverse=True)
Upvotes: 2
Reputation: 31339
You're consuming a generator. It's possible that it's has billions of items. If that's the case - python may be out of resources. Make sure you're not overloading the system by checking how big the resulting list will be.
I'd also take a look at problems with slicing in libraries that are used by networkx
(NumPy? SciPy?). Perhaps try:
CG1_nodes=list(nx.connected_components(G1))
To avoid slicing.
Upvotes: 2