i am constantly changing a variable in one module and trying to use that variable in another module.But I don't see the updated variable .
For eg.
I have check.py
total_time=0
I have check_1.py which increments total_time from check.py
import check
for i in range(6):
check.total_time +=1
sleep(4)
I have check_2.py which needs to use the incremented value
import check
for i in range(5):
print "in check 2 , total time is ", check.total_time
sleep(3)
I am running check_1 and check_2 at the same time .
check_1 keeps on increasing the value.
BUT check_2 always prints 0 whereas i am expecting it to print the updated increased value.
I am not sure what i am missing here .
Do I understand you right that you want to use a variable in a common module to transmit data between two separate Python scripts?
If this is what you assume would happen, you assume wrongly. Both of those scripts can definitely access the variable. Otherwise you would get an exception.
However, these scripts operate completely different memory segments that are independent of each other despite including the same Python module. Both programs have an independent copy of check.total_time and they will never see the variable of the other program.
There are a couple of possibilities to share data between scripts. Some of them are discussed here: How to share variables across scripts in python?
Related
I have this very nasty python script which is, to say the least not very well managed and I'd like to improve it. The way I thought about doing that is breaking up the code into what they do and then importing them into the main function that runs everything. But some of my defs have a global (like the key word based global) in them, how do I scope those out in separate files exactly?
for example main,py wi have:
import function
message = {}
while (true):
function.function(message)
print(message)
and function,py has :
def function(some variables) :
global message
if (somevariable = something):
message = "xyz"
message in the second file is giving me an error
EDIT: So I see that I need to give a bit more context, there are infact 3 processes (functions) that are running and the global message is what I'm using to pass information between all these threads. So Ideally Id like to separate all the processes as different files and then keep adding them as a new thread. How do I go about this?
I'm looking for the cleanest way to print a variable dynamically in python 3.
I want to repeatedly call psutil.virtual_memory() and print its return-value in place.
Something like this:
import psutil
import time
memory = psutil.virtual_memory()
while True:
print(f"Memory: {memory}")
time.sleep(0.5)
Ofcourse that wouldn't be dynamic. I want "memory" to update every 0.5 seconds on the same line.
I couldn't seem to find a clean example of how to do this in this manner.
UPDATE: I'm also wanting to learn how I can do this with multi-line print statements.
Like this
Memory: 500MB
CPU Usage: 25%
Just add print(..., end='\r') to the end of your printing statement.
This brings the cursor to the beginning of the line, so subsequent prints will overwrite it.
f-strings allow you to directly print a function call or expression in the first place, so do that. (No need to store the stale return value to a variable, in the first place)
while True:
print(f"Memory: {psutil.virtual_memory()}")
time.sleep(0.5)
UPDATE: after you accepted an answer, we infer that when you say "print the value in-place", you actually mean "overwrite the output (on console) from previous print"
"in-place" is actually a different term that usually refers to "variable assignment without allocating new memory". Better to say "overwrite on the console".
Add carriage return with flush for print.
import psutil
import time
def memory():
return psutil.virtual_memory()
while True:
print(f"Memory: {memory()}", end="\r", flush=True)
time.sleep(0.5)
display expression: prints out the value of an expression each time it gets changed. This is useful for monitoring the value of variables that get changed in loops. So, suppose the following is the code:
for i in range(100):
for j in range(100):
a=f(i,j)
I know something is wrong with the execution of a=f(i,j) for certain values of i and j. Then, how to use the display command from pdb module to find out the values of i and j when it does not work? I suppose when you use display command, it will display the value of i and j automatically, right? Do I need to combine the c command and b command from pdb module also? Many thanks for your time and attention.
display sets a "watch", so that each time execution stops (whether by completing a next, a step, or an until, or breaking on a continue ), if the value has changed, it will print a message showing the old value and the new value.
Since you know something is wrong with your f function, your easiest solution is to put a break on that function, and set display of the inputs inside that scope. Since you have shown us nothing about f, I don't know what the input variables will be called at that level, but it's likely that it won't be "i" and "j", so set the display appropriately.
I find display most useful for when I'm stepping through code that involves loops, using n or s or c. It keeps track of variables for me, and saves me from having to print the variables I'm interested in. If you know your problem is in f, you'll have to step through the code there yourself, and check all the variables at all the interesting statements. If you find yourself checking a variable repeatedly, that's where you use display.
I am trying to run my code in parallel using the python "from multiprocessing import Process, Value" model.However, I am creating a shared variable and using it as flag, so if one process find the result it will set the flag value to 1 and exit.Other processes should exit when the flag value is changed but for some reason the flag value did not change for these processes. this is the code:
from multiprocessing import Process, Value
gflag= Value('i',0)#this is the global flag shared between process
gflag=0
while True:
if not piece:
break
list1= piece.splitlines()
p = Process(target=Dowork, args=(gflag,inValue,list1,var1,))
p.start()
if(gflag==1):
exit()
piece = f.read(10000)
def doWork(gflag,inputvalue,diclist,var1):
for p in diclist:
calResult= doSomeCal(p,var1)
if( calResult == inputvalue):
gflag=1
exit()
if(gflag==1):
print"exit now"
exit()
Question is how to stop all the threads when on of them find the result?
The main problem with your attempted solution is that, because you assign to gflag within doWork, it's a local variable, not a global. To fix that, you need to add global gflag at the start of the function.
But even if you fixed that, it wouldn't solve your problem. When you write gflag=1, that doesn't update the shared value stored in gflag, it just rebinds the name gflag to the local int 1. You want to use gflag.value. (Note that once you fix this, the previous problem goes away… but it's still often better to use the global declaration for human readers, even when the compiler doesn't need it.)
And finally, doWork doesn't actually check the flag until after it's finished doing all the work, so the flag doesn't help you to exit the child processes earlier. You probably want to put that if statement inside the for loop, so it checks the flag once per element, instead of only once at the end.
I can't understand why the following codes gives different results:
from datetime import datetime
def foo():
return {datetime.now() : True}
a = {}
a.update(foo())
a.update(foo())
a.update(foo())
And:
a = {}
for i in xrange(3):
a.update(foo())
In the former a ends up with three elements, while in the later a ends up with just one element (from the last iteration).
Just your luck. The second code is likely to execute in a single millisecond (or microsecond, depending on your OS), and give three identical datetimes, first is less likely to do so (on your system/your way of executing code). Keep trying, you might end up with two elements. Don't write code that depends on luck.
The number of elements you end up with will depend on how many calls to foo Python executes within the granularity of datetime.now().
If you're running this in an interactive console then between statements executed at the prompt the console will perform housekeeping (for example, displaying the >>> prompt) that will significantly delay the next statement, whereas for the for loop the loop will be executed in its entirety before the console does anything.