Azure Blob 和队列线程安全
我需要一些帮助来理解 azure CloudBlobClient、CloudQueueClient 和 CloudBlob 类中的线程安全性。
我正在开发一个辅助角色,其中包括多个独立的作业处理器,其中每个作业处理器从特定队列读取数据并写入/更新到一些可能相同的 blob 容器。
我想确保这些作业处理器不会互相干扰。
1>在不使用任何类型的锁的情况下,如何确保是这种情况?如果我为每个作业处理器(都位于同一进程中)分配一个单独的 CloudBlobClient 和 CloudQueueClient,是否足以说它们彼此独立,并且因为每个作业处理器都使用单独的客户端实例,所以它们不会运行彼此之间到底有什么关系?
2>在同一作业处理器中,如果我尝试使用 Parallel.ForEach 在 CloudBlobClient 上实现并行性,以并行调用 GetBlobReference 或 UploadText,我是否需要合并某种同步,或者这些方法是线程安全的吗? Azure 文档说它们不是,但我在网上看到的大多数示例似乎都没有对这些方法应用任何类型的同步机制。实现这一目标的最佳方法是什么?我的意思是使用一个 CloudBlobClient 并并行调用 GetBlobReference 或 UploadText 的最佳方式?
I need some help in understanding the thread safety in azure CloudBlobClient, CloudQueueClient and CloudBlob classes.
I am developing a worker role which includes multiple independent job processors where each of these job processors read from a specific queue and write/update to some blob containers which may be the same.
I want to make sure that these job processors are not stepping on each other toe.
1> How can I make sure that this is the case without using any kind of lock? If I assign a separate CloudBlobClient and CloudQueueClient to each of my job processors (who all live within same process), is it enough to say that they are independent from each other and because every job processors uses a separate client instance, they will not run into each other at all?
2> Within same job processor, if I try to have parallelism on CloudBlobClient using Parallel.ForEach to say call GetBlobReference or UploadText in parallel, do I need to incorporate some sort of synchronization or are these methods thread safe? Azure documentation says they are not but most examples that I have seen online do not seem to apply any kind of synchronization mechanism on these methods. What is the best way to achieve this? I mean the best way to use one CloudBlobClient and call GetBlobReference or UploadText in Parallel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
随机选择函数的文档说“任何公共静态...这种类型的成员是线程安全的。任何实例成员都不能保证是线程安全的。”,但我认为这是放入的样板文件,因为有人懒得去考虑它。我建议您使用 Reflector 检查这些类,但我希望它们会很好,因为您的所有类引用只是属性的存储库,当您进行像 UploadText 这样的调用时,这些属性最终会被放入 HTTP REST 请求中。只要您不更改不同线程中一个对象的属性(并且我认为您不需要为 Parrallel.ForEach 的每次迭代创建一个 CloudBlobReference),那么我认为您就会安全。
The documentation for a randomly chosen function says "Any public static...members of this type are thread safe. Any instance members are not guaranteed to be thread safe.", but I would assume this is boilerplate put in because someone could not be bothered to think about it. I suggest you examine the classes using Reflector, but I would expect they would be fine because all the classes you refer to are just repositories for properties that end up being put into HTTP REST requests when you make a call like UploadText. So long as you do not change properties for one object in different threads (and I don't think you will need to - create a CloudBlobReference for each iteration of your Parrallel.ForEach) then I reckon you will be safe.
我查看了
CloudBlobClient
MSDN 上的文档 它说的是:由于这不是静态成员,因此不能保证线程安全。如果您想确保您不会被 MS 在存储客户端库中可能遗漏的任何线程问题所困扰,那么是的,您应该确保每个线程都有自己的客户端(也许创建一个 ThreadStatic 变量)。
话虽如此,我已经使用
CloudBlobClient
在Parallel.ForEach
中上传多个项目,而没有造成任何问题。I had a look at the
CloudBlobClient
documentation on MSDN and what it says is:As this is not a static member, it is not guaranteed to be thread safe. If you want to be sure that you're not going to get caught out by any threading issues the MS may have missed in the storage client library, then yes you should ensure that each thread has it's own client (maybe create a ThreadStatic variable).
Having said that, I have used the
CloudBlobClient
to upload multiple items in aParallel.ForEach
without it causing any problems.