我需要锁定一个文件以便用Python编写.它将同时从多个Python进程访问.我在网上找到了一些解决方案,但大多数都失败了,因为它们通常只基于Unix或基于Windows.
我在尝试使用时遇到了一个有趣的错误Unpickler.load(),这里是源代码:
open(target, 'a').close()
scores = {};
with open(target, "rb") as file:
unpickler = pickle.Unpickler(file);
scores = unpickler.load();
if not isinstance(scores, dict):
scores = {};
Run Code Online (Sandbox Code Playgroud)
这是追溯:
Traceback (most recent call last):
File "G:\python\pendu\user_test.py", line 3, in <module>:
save_user_points("Magix", 30);
File "G:\python\pendu\user.py", line 22, in save_user_points:
scores = unpickler.load();
EOFError: Ran out of input
Run Code Online (Sandbox Code Playgroud)
我想读的文件是空的.如何避免出现此错误,并获取空变量?
我正在使用搁置模块来保存一些数据.我的shelve对象的键值是设置对象.
酸洗片段:
import shelve
other_links = {
'Blue Estate The Game, 72': 'https://store.steampowered.com/account/ackgift/A5e5AB6C5050A331B?redeemer=esmcfofp%40skinsgifts.com',
'Hope Lake, 75': 'https://store.steampowered.com/account/ackgift/70E2E6Ce9F1733265?redeemer=esmcfofp%40skinsgifts.com',
'Forget Me Not: My Organic Garden, 74': 'https://store.steampowered.com/account/ackwift/A14373949D126B37?redeemer=esmcfofp%40skinsgifts.com',
'Until I Have You, 74': 'https://store.steampowered.com/account/ackgift/69030w5ECB8D0F07F?redeemer=esmcfofp%40skinsgifts.com',
'Lex Mortis, 74': 'https://store.steampowered.com/account/ackgift/2760w1D8B48EB3601?redeemer=esmcfofp%40skinsgifts.com',
'I am Bread, 72': 'https://store.steampowered.com/account/ackgift/1CEC5e2D2BEF20C41?redeemer=esmcfofp%40skinsgifts.com',
'Lumini, 72': 'https://store.steampowered.com/account/ackgift/472F108Aw0609C215?redeemer=esmcfofp%40skinsgifts.com'
}
links = shelve.open('links', writeback = True)
for key, value in other_links.items():
db_value = links.get(key)
if not db_value:
db_value = set()
db_value.add(value)
links[key] = db_value
links.close()
Run Code Online (Sandbox Code Playgroud)
在几次转储之后,引发了EOFError异常:
Exception in thread Thread-3:
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud)