如何解决缺少多个 ao.lock 的问题?
我正在编写一个简单的 pyS60 应用程序,之前没有真正用 python 做过任何事情或使用多线程,所以这对我来说有点新鲜。 为了保持应用程序打开,我在应用程序主体初始化后将 e32.Ao_lock 设置为 wait(),然后向 exit_key_handler 发出锁定信号。
该程序可能执行的任务之一是打开第三方应用程序 UpCode。 这会扫描条形码并将条形码字符串复制到剪贴板。 当我关闭 UpCode 时,我的应用程序应该恢复并粘贴剪贴板中的输入。 我知道这可以使用 Ao.lock 来完成,但我已经调用了它的一个实例。 理想情况下,我的应用程序会在注意到某些内容已粘贴到剪贴板后重新获得焦点。 我可以使用睡眠或定时器功能之一来完成我需要的功能吗?
您可以在此处找到完整的脚本,我已将其缩写为必要的部分如下:
lock=e32.Ao_lock()
# Quit the script
def quit():
lock.signal()
# Callback function will be called when the requested service is complete.
def launch_app_callback(trans_id, event_id, input_params):
if trans_id != appmanager_id and event_id != scriptext.EventCompleted:
print "Error in servicing the request"
print "Error code is: " + str(input_params["ReturnValue"]["ErrorCode"])
if "ErrorMessage" in input_params["ReturnValue"]:
print "Error message is: " + input_params["ReturnValue"]["ErrorMessage"]
else:
print "\nWaiting for UpCode to close"
#lock.signal()
# launch UpCode to scan barcode and get barcode from clipboard
def scan_barcode():
msg('Launching UpCode to scan barcode.\nPlease exit UpCode after the barcode has been copied to the clipboard')
# Load appmanage service
appmanager_handle = scriptext.load('Service.AppManager', 'IAppManager')
# Make a request to query the required information in asynchronous mode
appmanager_id = appmanager_handle.call('LaunchApp', {'ApplicationID': u's60uid://0x2000c83e'}, callback=launch_app_callback)
#lock.wait()
#print "Request complete!"
barcode = clipboard.Get()
return barcode
# handle the selection made from the main body listbox
def handle_selection():
if (lb.current() == 0):
barcode = scan_barcode()
elif (lb.current() ==1):
barcode = clipboard.Get()
elif (lb.current() ==2):
barcode = input_barcode()
found = False
if is_barcode(barcode):
found, mbid, album, artist = identify_release(barcode)
else:
msg('Valid barcode not found. Please try again/ another method/ another CD')
return
if found:
go = appuifw.query(unicode('Found: ' + artist + ' - ' + album + '\nScrobble it?'), 'query')
if (go == 1):
now = datetime.datetime.utcnow()
scrobble_tracks(mbid, album, artist, now)
else:
appuifw.note(u'Scrobbling cancelled', 'info')
else:
appuifw.note(u'No match found for this barcode.', 'info')
# Set the application body up
appuifw.app.exit_key_handler = quit
appuifw.app.title = u"ScanScrobbler"
entries = [(u"Scan a barcode", u"Opens UpCode for scanning"),
(u"Submit barcode from clipboard", u"If you've already copied a barcode there"),
(u"Enter barcode by hand", u"Using numeric keypad")
]
lb = appuifw.Listbox(entries, handle_selection)
appuifw.app.body = lb
lock.wait()
感谢所有帮助。
I'm programming a simple pyS60 app, not really done anything with python or using multiple threads before so this is all a bit new to me.
In order to keep the app open, I set an e32.Ao_lock to wait() after the body of the application is initialised, and then signal the lock on the exit_key_handler.
One of the tasks the program may do is open a third party app, UpCode. This scans a barcode and copies the barcode string to the clipboard. When I close UpCode, my application should resume and paste the input from the clipboard.
I know this can be accomplished using Ao.lock, but I've already called an instance of this. Ideally my application would regain focus after noticing something had been pasted to the clipboard.
Can I accomplish what I need with one of the sleep or timer functions?
You can find the full script here, and I've abbreviated it to the necessary parts below:
lock=e32.Ao_lock()
# Quit the script
def quit():
lock.signal()
# Callback function will be called when the requested service is complete.
def launch_app_callback(trans_id, event_id, input_params):
if trans_id != appmanager_id and event_id != scriptext.EventCompleted:
print "Error in servicing the request"
print "Error code is: " + str(input_params["ReturnValue"]["ErrorCode"])
if "ErrorMessage" in input_params["ReturnValue"]:
print "Error message is: " + input_params["ReturnValue"]["ErrorMessage"]
else:
print "\nWaiting for UpCode to close"
#lock.signal()
# launch UpCode to scan barcode and get barcode from clipboard
def scan_barcode():
msg('Launching UpCode to scan barcode.\nPlease exit UpCode after the barcode has been copied to the clipboard')
# Load appmanage service
appmanager_handle = scriptext.load('Service.AppManager', 'IAppManager')
# Make a request to query the required information in asynchronous mode
appmanager_id = appmanager_handle.call('LaunchApp', {'ApplicationID': u's60uid://0x2000c83e'}, callback=launch_app_callback)
#lock.wait()
#print "Request complete!"
barcode = clipboard.Get()
return barcode
# handle the selection made from the main body listbox
def handle_selection():
if (lb.current() == 0):
barcode = scan_barcode()
elif (lb.current() ==1):
barcode = clipboard.Get()
elif (lb.current() ==2):
barcode = input_barcode()
found = False
if is_barcode(barcode):
found, mbid, album, artist = identify_release(barcode)
else:
msg('Valid barcode not found. Please try again/ another method/ another CD')
return
if found:
go = appuifw.query(unicode('Found: ' + artist + ' - ' + album + '\nScrobble it?'), 'query')
if (go == 1):
now = datetime.datetime.utcnow()
scrobble_tracks(mbid, album, artist, now)
else:
appuifw.note(u'Scrobbling cancelled', 'info')
else:
appuifw.note(u'No match found for this barcode.', 'info')
# Set the application body up
appuifw.app.exit_key_handler = quit
appuifw.app.title = u"ScanScrobbler"
entries = [(u"Scan a barcode", u"Opens UpCode for scanning"),
(u"Submit barcode from clipboard", u"If you've already copied a barcode there"),
(u"Enter barcode by hand", u"Using numeric keypad")
]
lb = appuifw.Listbox(entries, handle_selection)
appuifw.app.body = lb
lock.wait()
Any and all help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过定义一个单独的第二个锁来解决这个问题,并确保一次只有一个在等待。 它似乎工作没有任何问题。 当前代码可以托管在 Google 代码上找到
I solved this problem by defining a separate second lock, and making sure only one was waiting at a time. It seems to work without any problem. Current code can be found hosted on google code