我想在多线程环境中使用Google。函数Files.Insert在DriveService类线程中安全吗?我在文档里找不到任何与此相关的东西。我甚至在那里找不到word 线程。
有人知道在Google中多线程是如何处理的吗?是否需要为每个线程创建单独的DriveService对象?
发布于 2013-04-19 22:32:44
我一直在Google上为使用它的winforms应用程序做一些性能测试。我看到了这篇文章,意识到我从来没有想过要检查这个问题,因为我总是认为API是线程安全的。所以我做了一些测试。我一直在对文件重命名方法进行一些性能测试,如下所示:
public static bool SetFileName(string gFileID, string filename, int attempt)
{
File sourcefile;
File replacement = new File();
bool save = false;
replacement.Title = filename;
replacement.Description = filename;
try
{
FilesResource.PatchRequest request = DriveService.Files.Patch(replacement, gFileID);
sourcefile = request.Fetch();
}
catch (Exception e)
{
if (attempt < 10)
{
return SetFileName(gFileID, filename, attempt + 1);
}
throw e;
}
bool result = sourcefile.Title == replacement.Title & sourcefile.Description == replacement.Description;
if(!result && attempt<10)
return SetFileName(gFileID, filename, attempt + 1);
return result;
}我在运行顺序测试,以获得该方法的错误率和执行时间。当我阅读您的文章时,我决定在后台工作人员上运行测试,以了解该方法在多线程环境中是如何工作的。在上面的示例中,DriveService对象实例化如下:
private static DriveService DriveService
{
get
{
if (_service == null)
{
try
{
_service = new DriveService(Auth);
}
catch (Exception)
{
}
}
return _service;
}
}正如您所看到的,我的DriveService对象只实例化一次,因此在多个后台工作人员同时运行的情况下,所有这些工作人员都在使用相同的DriveService实例。我使用1000个同时线程运行测试,虽然它确实生成了一些错误,但这些错误都是从google驱动器服务器返回的"(500)内部服务器错误“。看起来所有的文件名都被正确设置,所有线程都不挂起就完成了。
但是,在单独的性能测试中,我试图确定每次需要实例化DriveService时是否存在显着的性能问题。因此,我修改了我的代码如下:
private static DriveService DriveService
{
get
{
return new DriveService(Auth);
}
}我再次运行了一些性能测试,这一次我想看看在单个DriveService实例之间运行100个同时线程和为每个线程创建一个新的驱动器实例所花费的时间是否有显著的差异。我看不出在表现上有什么明显的差别。虽然在运行之间存在差异,但在我看来,这些变化与运行PatchRequest的服务器端处理时间有关,而且几乎与您是否在需要时创建DriveService的新实例无关。
我知道这并不是决定性的,但在我看来,Google是线程安全的。但是,如果不是,那么在将其视为线程安全时似乎没有任何性能问题。
我希望这能帮到你。
https://stackoverflow.com/questions/15437165
复制相似问题