1

I am using the matplotlib library for rendering charts and python-mapnik for maps out of a postgresql/postgis database. For each plot or map I use a single *.py script which works perfectly. However, if I run the python scripts from one single file (main.py) by using

execfile()

to execute all scripts via the commande line "main.py" crashes. I recognized that the CPU capacity drops and I have no clue how to solve this problem. It should be noted that there are some very large spatial queries which can take up to one minute.

I appreciate any help you can provide

zehpunktbarron
  • 1,193
  • 3
  • 15
  • 26
  • Why not *import* all those scripts? How are you running the `main.py` script, on what platform? What you do mean by *"main.py" crashes*? Does it simply exit, does the console it was running in close, do you get a traceback exception? – Martijn Pieters Aug 12 '13 at 08:02
  • main.py is executed from the commande line with some flags (db-connection, db-username, ...) on linux os (kubuntu). The console does neither close nor exit. All I can see is the cpu dropping to its "normal" level without printing any error message to the console. – zehpunktbarron Aug 12 '13 at 08:13
  • Then it doesn't crash. It exits normally. What happens if you add some print statements around the `execfile` statements? What about adding those into the other scripts? And why are you not using `import` for these? – Martijn Pieters Aug 12 '13 at 08:24
  • If I add print statements around execfile they are not printed to the command line. The same occurs when adding them to the other python scripts. I did not try to import all the scripts (52 in total) because I was recommended to use execfile. However, I'll test if importing the files works. – zehpunktbarron Aug 12 '13 at 08:45
  • So even the print statements **before** the `execfile` statements are not executed? How sure are you that the code *runs at all*? – Martijn Pieters Aug 12 '13 at 08:53
  • The code itself runs. I use a loop to execute each script and additionally print messages before and after the executed script. Each file generates a picture or a map and they are succesfully generated. After 7 files the message before the execution is printed but not the one after without any error message. If I only run the 7th file without the other 52 the script is executed without any problems... – zehpunktbarron Aug 12 '13 at 09:06
  • `execfile` executes the code in the *current* namespace, so your 52 scripts can interact with one another in unexpected ways. If script number 7 contains code that exits the interpreter (`sys.exit()` for example) then the main program exits, etc. What does that 7th program do? You may want to share that code here. – Martijn Pieters Aug 12 '13 at 09:09
  • My 7th programm executes a massive spatial query on the database and returns the data to plot a diagram. If I run the 7th programm on, let's say the second position (all 52 do not depend on each other), this script is executed succesfully but, however, stops at another script (e.g. 10th) – zehpunktbarron Aug 12 '13 at 09:37
  • 1
    So the scripts **still** interact with one another *because the namespaces are not independent*. Use `subprocess.call([sys.executable, path_to_script])` or re-tool for importing instead. – Martijn Pieters Aug 12 '13 at 09:43

0 Answers0