How can you make Python code run Multiple Times for SUMO? - python

I have a traffic simulation that is running in the simulation tool known as SUMO and I am trying to have the simulation run repeatedly multiple times (about 50 times at the most), but for the sake of this example I will use 10 times. My main code is in another Python file and it requires that arguments be passed into it prior to running. This code is quite lengthy in nature and has multiple Python functions in it so I will not post it in this forum, but for sake of simplification lets call it performSIM.py.
My other Python code that is responsible for repeating the main code looks something like this:
import traci
from sumolib import checkBinary
import argparse
import performSIM
for i in range(10):
pythonFile = str(performSIM)
exec(pythonFile)
I was wondering what is the right way to loop my main code so that runs the simulation 10 times repeatedly? When I run the lines of code above I get the following error:
Traceback (most recent call last):
File "C:\Users\#####\Sumo\USA Road Network SUMO_2021\running_Python.py", line 8, in <module>
exec(pythonFile)
File "<string>", line 1
<module 'performSIM' from 'C:\\Users\\#####\\Sumo\\USA Road Network
SUMO_2021\\performSIM.py'>
**SyntaxError: invalid syntax**
How exactly can I fix this issue so this error does keep showing up?
Thank you in advance for the help

Your performSIM should have one main function, if it does not have one yet you should restructure it in such a way that it does. A common pattern is to pass the command line arguments to the main function as a parameter. So performSIM.py could look like this:
import sys
def main(args):
# parse the args
# do other useful stuff
if __name__ == "__main__":
main(sys.argv)
Then your other script can simply do
import sys
import performSIM
for i in range(10):
performSIM.main(sys.argv)

Related

Unittest hangs when testing input, but same input runs perfectly fine on main program

Whenever I run the following unittest, it just hangs and nothing happens. The program itself returns an output withing 1 sec, but I've ran this for 5+ mins without anything happening.
import unittest
from syntax import *
class SyntaxTest(unittest.TestCase):
def testCase(self):
self.assertEqual(CheckSyntax('C'), 'Formeln är syntaktiskt korrekt')
if __name__ == '__main__':
unittest.main()
The code itself is a simple syntax checker for molecules. It's not completely finished yet, but runs fine for the specified input above ('C'). I've uploaded it here: https://pastebin.com/rD3f6PWL
You are importing your syntax module, that means it will run all the function and class definitions (not the code inside them) and any other code in the main body. the last line of the syntax module is
print(CheckSyntax(input()))
So when you import the syntax module this line will be executed and will be waiting for input. If you only mean this line to be executed when you run syntax as a script then you would be better to wrap it in an if main block like
if __name__ == "__main__":
print(CheckSyntax(input()))
this way the last line will only be run if the script is being run directly and not if its being imported to be used somewhere else.

Python3 script to get status of deluge

I want to make a python script that every 30 minutes checks the status of a torrent on my RPI (where deluge is up and running) and if completed sends an email. I know how to make a timer, loop and email, but I don't know about deluge. I've read this: https://media.readthedocs.org/pdf/deluge/latest/deluge.pdf.
I know I need to import deluge and start with get_session_state() and get it to string, but it doesn't work.
Here's the error:
Traceback (most recent call last):
File "torrents_status.py", line 3, in <module>
get_session_state()
NameError: name 'get_session_state' is not defined
If you are just doing import deluge, you have to use the full module name for the methods.
deluge.get_session_state()
However, I'm imagining there is some connection object to the session that you should actually be calling that method on. I don't want to read through that 300 page manual, though, to find the correct module you should import.
Alternatively, you could do something like
from deluge import get_session_state
Then call
get_session_state()
Once again, you'll need to use the correct module name instead of deluge.

Make _tkinter.createfilehandler work again (or provide a workaround)

I've got some old Python scripts which used a different version of tkinter than the current systems are equipped with. Back in the old days, a method named _tkinter.createfilehandler() existed and worked. With the current versions this returns with a RuntimeError exception:
Traceback (most recent call last):
File "src/text.py", line 513, in <module>
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
RuntimeError: _tkinter.createfilehandler not supported for threaded Tcl
The script I'm trying to get to run is this (shortened version of course):
#!/usr/bin/env python
import os
from Tkinter import *
(pipe_r, pipe_w) = os.pipe()
# ...
def drain_pipe(a, b):
# handle data from pipe_r
# ...
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
tk.mainloop()
Is there a simple way to get this running again? I'm sure there is a way to convert the scripts (or maybe write them anew based on a different paradigm), but I'd be happy with a hack to not have to change very much (maybe there's a switch to enable this again somehow) because I've got several such scripts and would prefer not to have to patch a lot.
If tk is a Tk() object, then use tk.createfilehandler instead.

second python execution fails

I'm having a problem embedding the python 3 engine for an app that need to run custom scripts in python. Since the scripts might be completely different, and sometimes user provided, I am trying to make each execution isolated and there is not need to preserve any data between execution of the different scripts.
So, my solution is to wrap each execution between Py_Initialize and Py_Finalize. It looks something like that:
void ExecuteScript(const char* script)
{
Py_Initialize();
PyRun_SimpleString( script );
Py_Finalize();
}
However, this fails for a particular python script the second time a script is executed with:
done!
Traceback (most recent call last):
File "<string>", line 8, in <module>
File "\Python33Test\Output\Debug\Python33\Lib\copy.py", line 89, in copy
rv = reductor(2)
TypeError: attribute of type 'NoneType' is not callable
The python script looks like this:
class Data:
value1 = 'hello'
value2 = 0
import copy
d = Data()
dd = copy.copy( d )
print ( 'done!' )
As you can see, the first time around the script was executed the 'done!' was printed out. But the second time it rises an exception inside the copy function.
It looks like the python engine was left in some weird state after the first initialize-finalize. Note, this is python 3.
Also, it is very interesting to note that Python 2.7 did not have this problem.
I guess there might be other examples that could reveal better what's going, but i haven't had the time to find yet.
Full sources of the test project can be found here:
https://docs.google.com/file/d/0B86-G0mwwxZvNGpoM1Jia3E2Wmc/edit?usp=sharing
Note, the file is 8MB because it includes the python distribution.
Any ideas of how to solve this are appreciated.
EDIT: I also put a copy of the project containing flag to switch between Python 3 and Python 2.7 (the file is 31 MB): https://docs.google.com/file/d/0B86-G0mwwxZvbWRldTd5b2NNMWM/edit?usp=sharing
EDIT: Well, I tested with Python3.2 and it worked fine. So it seems to be bug in Python3.3 only. Adding as an issue: http://bugs.python.org/issue17408#
Well, this was fast.
They actually found the issue being a problem in Python 3.3 code.
http://bugs.python.org/issue17408#

Mysterious pickle error while profiling a multi-process Python script [duplicate]

This question already has an answer here:
cProfile causes pickling error when running multiprocessing Python code
(1 answer)
Closed 11 months ago.
I"m using the multiprocessing module, and I'm using UpdateMessage objects (my own class), sent through multiprocessing.Queue objects, to communicate between processes. Here's the class:
class UpdateMessage:
def __init__(self, arrayref, rowslice, colslice, newval):
self.arrayref = arrayref
self.rowslice = rowslice
self.colslice = colslice
self.newval = newval
def do_update(self):
if self.arrayref == 'uL':
arr = uL
elif self.arrayref == 'uR':
arr = uR
else:
raise Exception('UpdateMessage.arrayref neither uL nor uR')
arr[self.rowslice, self.colslice] = self.newval
When I run the script, it works perfectly fine. However, when I run it with either cProfile or profile, it gives the following error:
_pickle.PicklingError: Can't pickle <class '__main__.UpdateMessage'>: attribute lookup __main__.UpdateMessage failed
It seems to be trying to pickle the class, but I can't see why this is happening. My code doesn't do this, and it works just fine without it, so it's probably the multiprocessing module. But why would it need to pickle UpdateMessage, and how can I fix the error?
EDIT: here's part of the code that's sending the UpdateMessage (multiple parts of the script do this, but all in the same way):
msg = UpdateMessage(uLref, refer[0] + marker[0] - 2,
slice(uL.shape[1]), ustar.copy())
queue.put(msg)
The traceback isn't very helpful:
Traceback (most recent call last):
File "/usr/lib/python3.2/multiprocessing/queues.py", line 272, in _feed
send(obj)
I do not know how your processes look like but:
'__main__.UpdateMessage'
refers to UpdateMessage in the launched module.
When another process is started not with the Module of the class UpdateMessage, UpdateMessage will not be available.
you have to import that module that includes UpdateMessage so
UpdateMessage.__module__ is not '__main__'.
Then Pickle can find UpdateMessage also in other programs.
I recommend the __main__.py to look like this:
import UpdateMessage_module
UpdateMessage_module.main()
I had the same problem, and solved it by defining the class to be pickled in its own file.
I'm assuming the pickling attempt on your class is happening before your class is sent to another process. The easiest solution is probably just to implement the pickle-protocol explicitly on your class...

Categories