Skip to content

Instantly share code, notes, and snippets.

@prideout
Created May 22, 2012 02:33
Show Gist options
  • Save prideout/2766158 to your computer and use it in GitHub Desktop.
Save prideout/2766158 to your computer and use it in GitHub Desktop.
Calling Python from Tcl
#!/usr/bin/python
import itertools as it
class pi_computing_device(object):
def __init__(self, iterations):
self.n = iterations
def compute_pi(self):
coefficients = it.cycle((1, -1))
divisors = it.islice(it.count(1), 0, self.n, 2)
return 4*sum(c * 1.0/d for c, d in it.izip(coefficients, divisors))
# Open a file stream to an interactive Python session
set pystream [open "|/usr/bin/python -i wombat.py" r+]
fconfigure $pystream -blocking 0
# Ask Python to calculate Pi
puts $pystream "w = pi_computing_device(1000)"
puts $pystream "print w.compute_pi()"
flush $pystream
# Spin until we get a response from Python
# Probably better to use "filevent"
while {1} {
set L [gets $pystream foo]
# Print it to the screen in Tcl
if {$foo != ""} {
puts "Pi is approximately equal to $foo"
break
}
}
# Close the stream to shut down Python
close $pystream
@MarkoPolo15
Copy link

Hi , this is already an old post I guess .
But I am stuked , and hope I can get some help here.
I am running Phyton fron TCL .
The difference between this case and mine is that I must work on same session (so no close session is allowed ) .
Now the issue is , that Python keeps on getting same commands over and over from TCL. I am sending each time different commands .
My phyton opens an ssh stream .
First line is puts $pystream "ls -l" .
As expected getting the correct output .
Next sending put $pystream "some other command" . And still getting same response as the previous . I am using the flush, as mentioned .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment