我不熟悉asyncore,并且对异步编程的知识非常有限,除了一些扭曲的教程介绍.
我最熟悉线程并在我的所有应用程序中使用它们.一个特定的应用程序使用couchdb数据库作为其接口.这涉及对数据库进行longpolling以寻找更改和更新.我用于couchdb的模块是couchdbkit.它使用asyncore循环来监视这些更改并将它们发送到回调.
所以,我从这个回调中发现了我启动工作线程的地方.混合异步和线程编程似乎有点粗糙.我真的很喜欢couchdbkit,但不想在我的程序中引入问题.
所以,我的问题是,从异步回调中触发线程是否安全?
这是一些代码......
def dispatch(change):
global jobs, db_url # jobs is my queue
db = Database(db_url)
work_order = db.get(change['id']) # change is an id to the document that changed.
# i need to get the actual document (workorder)
worker = Worker(work_order, db) # fire the thread
jobs.append(worker)
worker.start()
return
main()
.
.
.
consumer.wait(cb=dispatch, since=update_seq, timeout=10000) #wait constains the asyncloop.
Run Code Online (Sandbox Code Playgroud)
更新:
在仔细研究了这个之后,我还有一个关于couchdbkit大师的问题.使用该数据库可能有数百个线程.正如您在我的代码示例中所看到的,我正在为每个线程实例化一个couchdbkit.Database对象.我认为这可能是浪费.那么,在线程中全局使用单个数据库对象是否可以?
我有我认为是一个常见的问题:一些托管bean有一个动作,它向上下文添加一些消息:
FacesMessage fm = new FacesMessage("didn't work");
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, fm);
return "some-outcome";
Run Code Online (Sandbox Code Playgroud)
然后我将结果映射faces-config.xml
并配置为
<navigation-case>
<from-outcome>some-outcome</from-outcome>
<to-view-id>/view.xhtml</to-view-id>
<redirect/>
</navigation-case>
Run Code Online (Sandbox Code Playgroud)
在view.xhtml
我提出的信息:
<h:message globalsOnly="true" />
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用,因为执行重定向时消息会丢失.
我该如何解决?我发现这个惊人的帖子解释了如何使用a来做,PhaseListener
但我相信这种情况太常见了,不能用这种方式来解决.我错了吗?我应该创建PhaseListener
吗?或者是否有其他标准解决方案?
是否可以下载所有Cocoa API文档?我必须离线工作很多次,有时怀疑会阻止我的所有工作......
例如,为什么这有效?
def func1(func1var):
def innerfunc(innerfuncvar):
if func1var == 1:
print innerfuncvar
else:
print 5
func2(innerfunc)
def func2(function):
function(9)
Run Code Online (Sandbox Code Playgroud)
当innerfunc
被调用时func2
,它如何知道func1var
?的值?
我有一个由Mercurial版本的项目.有一次,我跑了一个命令如...
$ find . -type f | xargs sed -i.bkp 's/my_func/another_func/'
Run Code Online (Sandbox Code Playgroud)
......在这个项目中.然后Mercurial停下来继续工作:
$ hg status
abort: index 00changelog.i is corrupted!
Run Code Online (Sandbox Code Playgroud)
该文件00changelog.i
不包含替换的字符串,即使我移动00changelog.i.bkp
到00changelog.i
问题仍然存在.hg verify
没有帮助:
$ hg verify
abort: index 00changelog.i is corrupted!
Run Code Online (Sandbox Code Playgroud)
我通过从另一个目录中的远程存储库克隆项目然后.hg
从我的克隆存储库复制到损坏的存储库来解决了这个问题.但是,我想知道:还有另一种更实用的解决方法吗?顺便说一句,如果"损坏的"文件甚至没有改变,为什么会出现这个问题呢?
我真的很困惑.我基本上试图用机械化为python填写网站上的表格.除了下拉菜单,我得到了一切工作.我用什么来选择它以及我为该值添加了什么?我不知道我是否应该把选择的名称或它的数值.非常感谢帮助,谢谢.
代码段:
try:
br.open("http://www.website.com/")
try:
br.select_form(nr=0)
br['number'] = "mynumber"
br['from'] = "herpderp@gmail.com"
br['subject'] = "Yellow"
br['carrier'] = "203"
br['message'] = "Hello, World!"
response = br.submit()
except:
pass
except:
print "Couldn't connect!"
quit
Run Code Online (Sandbox Code Playgroud)
我在运营商遇到了麻烦,这是一个下拉菜单.
我希望以这种方式注入配置参数:
public class MyManagedBean {
@Inject
public MyManagedBean(@Named("user") String user){
....
}
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试用这种方式实现一个生成器方法:
@ApplicationScoped
public class MyConfiguration {
private Properties loadProperties() {
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(
"user.properties"));
} catch (IOException e) {
throw new RuntimeException();
}
return properties;
}
@Produces
@Named("user")
String getUser() {
return loadProperties().getProperty("user");
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样定义的其他bean:
public class OtherManagedBean {
@Inject
public OtherManagedBean(MyManagedBean myManagedBean){
....
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试部署它时,我遇到了这个异常:
INFO: WEB0671: Loading application [example-ear#example-war.war] at [example]
SEVERE: Exception while loading the app
SEVERE: …
Run Code Online (Sandbox Code Playgroud) 我正在通过Udacity和Dave Evans介绍了关于列表属性的练习
list1 = [1,2,3,4]
list2 = [1,2,3,4]
list1=list1+[6]
print(list1)
list2.append(6)
print(list2)
list1 = [1,2,3,4]
list2 = [1,2,3,4]
def proc(mylist):
mylist = mylist + [6]
def proc2(mylist):
mylist.append(6)
# Can you explain the results given by the four print statements below? Remove
# the hashes # and run the code to check.
print (list1)
proc(list1)
print (list1)
print (list2)
proc2(list2)
print (list2)
Run Code Online (Sandbox Code Playgroud)
输出是
[1, 2, 3, 4, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]
[1, 2, 3, …
Run Code Online (Sandbox Code Playgroud) myVar = ["jhhj", "hgc"]
myTuple = ([1,2,3], [4,5,6], myVar)
myVar.append('lololol')
print myTuple
Run Code Online (Sandbox Code Playgroud)
为什么以及如何通过在施工后附加来修改这个元组?
myVar = "lol"
myTuple = ([1,2,3], [4,5,6], myVar)
myVar = "lolol"
print myTuple
Run Code Online (Sandbox Code Playgroud)
为什么打印出来([1,2,3], [4,5,6], "lol")
而不是([1,2,3], [4,5,6], "lolol")
?