GVKun编程网logo

在推理服务中使用tf.Session时,线程安全吗?

13

针对在推理服务中使用tf.Session时,线程安全吗?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c–`asprintf`线程安全吗?、c#–RestSharp线程安全吗?、c#–这是使

针对在推理服务中使用tf.Session时,线程安全吗?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c – `asprintf`线程安全吗?、c# – RestSharp线程安全吗?、c# – 这是使用静态队列线程安全吗?、DbContext线程安全吗?等相关知识,希望可以帮助到你。

本文目录一览:

在推理服务中使用tf.Session时,线程安全吗?

在推理服务中使用tf.Session时,线程安全吗?

现在我们已经使用TensorFlow训练和导出模型。我们可以像使用方法tensorflow/serving一样使用此模型来实现推理服务。

我有一个关于该tf.Session对象是否是线程安全的问题。如果为true,我们可以在启动后初始化该对象,并使用singleton对象处理并发请求。

答案1

小编典典

tf.Session对象对于Session.run()来自多个线程的调用是线程安全的。

在TensorFlow
0.10之前,图形修改不是线程安全的。这个问题在0.10版本中已修复,因此您可以在Session.run()调用的同时添加节点到图中,尽管出于性能原因不建议这样做。相反,建议sess.graph.finalize()在从多个线程使用会话之前进行调用,以防止意外的内存泄漏。

c – `asprintf`线程安全吗?

c – `asprintf`线程安全吗?

GNU函数asprintf(打印到分配的字符串)线程安全吗?

(IIC,基本上,这归结为malloc是否是线程安全的问题.)

考虑示例代码:

#define _GNU_SOURCE
#include <stdio.h>

#include "getValue.h"

char * getValue(int key) {
  char * value;
  asprintf(&value,"%d",key); // Todo: No error handling!
  // If memory allocation wasn't possible,or some other error occurs,these  functions  will
  // return -1,and the contents of strp is undefined.
  return value;
}

在这里,我不触及任何全局变量.如果我的getValue在并发线程中被调用怎么办?没有坏事会发生,他们会吗?

解决方法

是的,它是线程安全的,除非它读取区域设置.

asprintf

Function: int asprintf (char **ptr,const char *template,…)
Preliminary: | MT-Safe locale | AS-Unsafe heap | AC-Unsafe mem

关于’locale’exception,特别是:

Functions annotated with locale as an MT-Safety issue read from the locale object without any form of synchronization. Functions annotated with locale called concurrently with locale changes may behave in ways that do not correspond to any of the locales active during their execution,but an unpredictable mix thereof.

这些类型的函数被称为“conditionally”多线程安全,因为在某些情况下,它们结果并非如此,因此程序员需要处理这些问题.

c# – RestSharp线程安全吗?

c# – RestSharp线程安全吗?

在尝试通过REST向网站发布数据失败后,我决定考虑使用库来实现这一目标.

RestSharp似乎是显而易见的选择.然而,经过一番挖掘,我发现了this issue.

我无法在RestSharp谷歌组和GitHub上找到有关此问题的任何更新的证据.

在最后一次发布之前报告了这个问题,但我再也无法确定问题是否已得到修复.

有谁知道问题是否已修复?

解决方法

似乎答案是否定的. RestClient不是线程安全的,至少在此问题关闭之前:

https://github.com/restsharp/RestSharp/issues/951

c# – 这是使用静态队列线程安全吗?

c# – 这是使用静态队列线程安全吗?

msdn文档指出静态通用队列是线程安全的.这是否意味着以下代码是线程安全的?换句话说,当一个线程启动一个int和另一个线程时,是否有同时出现一个int的问题?我必须锁定线程安全的入队和出队操作吗?
class Test {
    public static Queue<int> queue = new Queue<int>(10000);

    Thread putIntThread;
    Thread takeIntThread;

    public test() {
        for(int i = 0; i < 5000; ++i) {
            queue.Enqueue(0);
        }
        putIntThread = new Thread(this.PutInt);
        takeIntThread = new Thread(this.TakeInt);
        putIntThread.Start();
        takeIntThread.Start();
    }

    void PutInt() {
        while(true)
        {
            if(queue.Count < 10000) {//no need to lock here as only itself can change this condition
                queue.Enqueue(0);
            }
        }
    }

    void TakeInt() {
        while(true) {
            if(queue.Count > 0) {//no need to lock here as only itself can change this condition
                queue.Dequeue();
            }
        }
    }

}

编辑:我必须使用.NET 3.5

解决方法

这绝对不是线程安全的.从 Queue<T>的文件.

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A Queue<T> can support multiple readers concurrently,as long as the collection is not modified. Even so,enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration,you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing,you must implement your own synchronization.

重读你的问题,你似乎对这个短语“这种类型的静态成员”感到困惑 – 这不是说“静态队列”,因为没有这样的事情.一个对象不是静态的 – 一个成员是.当谈到静态成员时,它谈论的是Encoding.GetEncoding(Queue< T>实际上没有任何静态成员).实例成员是Enqueue和Dequeue – 与类型实例相关的成员,而不是类型本身.

因此,您需要为每个操作使用锁定,或者如果您使用的是.NET 4,请使用ConcurrentQueue<T>.

DbContext线程安全吗?

DbContext线程安全吗?

我想知道DbContext该类是否是线程安全的,我以为不是,因为我当前正在执行可访问DbContext我的应用程序中的paralell线程,并且得到了大量的锁定异常和其他看起来像它们与线程相关的东西。

直到最近我还没有收到任何错误…但是直到最近我才DbContext在线程中访问。

如果我是对的,人们会提出什么建议呢?

答案1

小编典典

它不是线程安全的。只需DbContext在您的线程中创建一个新的实例。

关于在推理服务中使用tf.Session时,线程安全吗?的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c – `asprintf`线程安全吗?、c# – RestSharp线程安全吗?、c# – 这是使用静态队列线程安全吗?、DbContext线程安全吗?的相关知识,请在本站寻找。

本文标签: