我想知道是否有办法从bash进程设置环境变量并从另一个进程读取它.
由于环境变量的值是进程的本地值(除继承之外),因此不能只export FOO="bar"在终端中执行并从另一个终端读取它.然后我试图让他们通过/proc/environ,但这是我得到的:
etuardu@subranu:~$ FOO="foo" bash
etuardu@subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu@subranu:~$ export FOO="bar"
etuardu@subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu@subranu:~$ echo $FOO
bar
Run Code Online (Sandbox Code Playgroud)
看来我可以在进程启动时获得该环境变量的值.
它的当前价值如何?
我有这个python代码,它将几个lambda函数放在一个字典中:
dictfun = dict()
for txt in ("a", "b", "c"):
dictfun[txt] = lambda: "function " + txt
Run Code Online (Sandbox Code Playgroud)
这些函数只返回一个字符串,其中包含调用它们的参数.
我希望看到像function afor dictfun["a"](),function bfor dictfun["b"]()等的输出,但这就是我得到的:
>>> dictfun["a"]()
'function c'
>>> dictfun["b"]()
'function c'
>>> dictfun["c"]()
'function c'
Run Code Online (Sandbox Code Playgroud)
它们似乎都评估txt到它设置的最后一个值,即它的当前值.总之,变量不会关闭到lambdas中.我甚至可以这样做:
>>> txt = "a"
>>> dictfun["c"]()
'function a'
Run Code Online (Sandbox Code Playgroud)
我如何才能关闭txtlambda函数以获得预期的输出?
什么是停止线程并等待另一个线程执行一定次数的语句(或方法)的最佳方法?我正在考虑这样的事情(让"数字"成为一个int):
number = 5;
while (number > 0) {
synchronized(number) { number.wait(); }
}
...
synchronized(number) {
number--;
number.notify();
}
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,首先是因为看起来你不能在int类型上等待().此外,对于我这样一个简单的任务来说,所有其他解决方案对我的Java天真的想法来说都非常复杂.有什么建议?(谢谢!)
我有tkinter类和其中的一些函数,(假设所有其他函数都存在以启动GUI).我做了什么我已经启动了一个self.function作为来自其他self.function和线程函数的线程在出错时我想使用tkMessageBox.showerror('Some Error')但这不适用于线程函数和我的程序得到了卡住.msgbox正在从事其他功能.
import threading
from Tkinter import *
import Pmw
import tkMessageBox
class tkinter_ui:
def __init__(self, title=''):
... assume all functions are present ...
def login(self, username, password)
if password == "":
tkMessageBox.showerror('Login Error', 'password required') # but on this msg box program become unresponsive why???
def initiateLogin(self)
tkMessageBox.showinfo('Thread', 'Started') #you see this msg box works
self.t = threading.Timer(1, self.login)
self.t.start()
Run Code Online (Sandbox Code Playgroud) 我想避免使用update()方法,并且我读到可以使用"+"操作数将两个字典合并到第三个字典中,但在我的shell中发生的是:
>>> {'a':1, 'b':2}.items() + {'x':98, 'y':99}.items()
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
{'a':1, 'b':2}.items() + {'x':98, 'y':99}.items()
TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
>>> {'a':1, 'b':2} + {'x':98, 'y':99}
Traceback (most recent call last):
File "<pyshell#85>", line 1, in <module>
{'a':1, 'b':2} + {'x':98, 'y':99}
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它发挥作用?
有时我需要临时注释掉块标题以进行测试,例如:
i = 2
s = { 'a', 'b', 'c' }
#while i > 0:
s.pop()
i -= 1
print(s)
Run Code Online (Sandbox Code Playgroud)
但是,由于缩进是python语法的一部分,如果我运行上面的代码,我得到:
s.pop()
^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
我知道对注释中的代码进行dedenting while会使它工作,但是我想保留代码的可视化结构,而不是每次都进行dedenting和缩进.
有什么技巧可以做到这一点吗?
python ×3
bash ×1
closures ×1
commenting ×1
dictionary ×1
dictview ×1
indentation ×1
java ×1
lambda ×1
process ×1
procfs ×1
python-3.x ×1
synchronized ×1
tkinter ×1
unix ×1