如何优雅的进行异步编排

news/2024/7/27 9:02:00/文章来源:https://blog.csdn.net/weixin_47552725/article/details/135607237

Future模式

Future模式是高并发设计和开发过程中常见的设计模式。它的核心思想是异步调用,对于Future模式来说,它不是立即返回我们所需要的结果,但是它会返回一个异步任务,将来我们可以通过这个异步任务获取到我们所需要的结果。

在进行传统的RPC调用时,同步RPC调用是一段相当耗时的过程。当前客户端发出RPC请求,服务端处理客户端的RPC请求并返回服务端的过程需要一定的时间,在这个过程中客户端一直在等待,直到服务端结果的返回。如下图:

假设一次远程调用的时间为500毫秒,则一个Client同步对三个Server进行RPC调用的时间为1500毫秒。可以使用Future模式对其进行改造,将同步的RPC调用改为异步的RPC调用,一个Client异步对三个Server调用的流程如下

假设一次远程调用的时间为500,600,700毫秒,则一个Client异步并发对三个Server分别进行一次RPC调用的总时间只要耗费700毫秒。使用Future模式异步并发地进行RPC调用,客户端在得到一个RPC的返回结果前并不急于获取该结果,而是充分利用等待时间去执行其他的耗时操作(如其他RPC调用),这就是Future模式的核心所在。

Java的Future实现类并没有支持异步回调,仍然需要主动获取异步执行的结果,而Java8的CompletableFuture组件实现了异步回调的功能

在Java中,Future只是一个泛型接口,位于Java.util.concurrent包下,其中定义了5个方法,主要包括一下几个功能

public interface Future<V> {//取消异步执行boolean cancel(boolean mayInterruptIfRunning);//获取异步任务的取消状态boolean isCancelled();//判断异步任务是否执行完成boolean isDone();//获取异步任务完成后的执行结果(阻塞获取)V get() throws InterruptedException, ExecutionException;//设置时限,获取异步任务完成后的执行结果V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException;
}

总体来说,Future是一个对异步任务进行交互、操作的接口。但是Future仅仅是一个接口,通过它没有办法直接对异步任务操作,JDK提供了一个默认的实现类--FutureTask。

CompletableFuture详解

CompletableFuture是JDK8引入的实现类,实现了Future和CompletionStage接口。该实现类的实例作为一个异步任务,可以在自己的异步任务执行完成之后再出发其他的异步任务,从而达到异步回调的目的。

1.CompletableFuture的UML类关系

其中ComCompletionStage代表计算过程中的某个阶段(异步任务),一个阶段完成之后可以进入下一个阶段。一个阶段可以理解为一个异步任务,其中每一个异步任务都封装了函数式接口,表示该异步任务需要执行的操作。

2.CompletionStage接口

CompletionStage代表某个同步或异步计算的一个阶段,或者一系列异步任务中的一个子任务(或者阶段性任务)。

每个CompletionStage子任务所包装的可以是一个Function、Consumer或者Runnable函数式接口实例。这三个常用的函数式接口的特点如下:

(1)Function

Function接口的特点是:有输入、有输出。包装了Function实例的CompletionStage子任务需要一个输入参数,并会产生一个输出结果到下一步。

(2)Runnable

Runnable接口的特点是:无输入、无输出。包装了Runnable实例的CompletionStage子任务既不需要任何输入参数,又不会产生任何输出。

(3)Consumer

Consumer接口的特点是:有输入、无输出。包装了Consumer实例的CompletinStage子任务需要一个输入参数,但不会产生任何输出。

CompletionStage代表异步计算过程中的某一个阶段,一个阶段完成以后可能会触发另一个阶段。虽然一个子任务可以触发其他子任务,但是并不能保证后续子任务的执行顺序

3.使用runAsync和supplyAcync创建子任务

CompletionStage子任务是通过创建CompletableFuture完成的。CompletableFuture提供了非常强大的Future扩展功能来帮助使用者简化异步编程的复杂性,提供了函数式编程的能力来帮助回调,也提供了转换和组合CompletionStage的能力。

CompletableFuture定义了一组创建CompletionStage子任务的方法,基础使用如下

  	//子任务包装一个Supplier实例,并调用ForkJoinPool.commonPool()线程来执行public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)//子任务包装一个Supplier实例,并使用指定的executor线程池来执行public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor)//子任务包装一个Runnable实例,并调用ForkJoinPool.commonPool()线程来执行public static CompletableFuture<Void> runAsync(Runnable runnable)//子任务包装一个Runnable实例,并使用指定的executor线程池来执行public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor)
//在使用CompletableFuture创建CompletionStage子任务时,
//如果没有指定Executor线程池,在默认情况下CompletionStage会使用公共的ForkJoinPool线程池。

两个创建CompletionStage子任务的示例如下:


public class CompletableFutureDemo {//创建一个无消耗值(无输入值)、无返回值的异步子任务public static void runAsyncDemo() throws Exception {CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {Thread.sleep(2000);System.out.println("run end...");} catch (InterruptedException e) {throw new RuntimeException(e);}});//等待异步任务执行完成,限时等待1秒://等待时间完成之后,判断异步任务是否执行完成,如果未完成则抛出超时异常future.get(1, TimeUnit.SECONDS);}//创建一个无消耗值(无输入值)、有返回值的异步子任务public static void supplyAsyncDemo() throws Exception {CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {long start = System.currentTimeMillis();try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("run end...");return System.currentTimeMillis() - start;});//等待异步任务执行完成,限时等待2秒long time = future.get(2, TimeUnit.SECONDS);System.out.println("异步执行耗时(秒):" + time / 1000);}public static void main(String[] args) throws Exception {runAsyncDemo();supplyAsyncDemo();}
}
4.设置子任务回调钩子

可以为CompletionStage设置回调钩子,当计算完成或任务抛出异常时执行这些回调钩子。

设置子任务回调钩子的主要函数如下

//设置子任务完成时的回调钩子
public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
//设置子任务完成时的回调钩子,可能不在同一线程执行
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
//设置子任务完成时的回调钩子,提交给线程池executor执行
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) 
//设置异常处理的回调钩子
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)

下面是一个CompletionStage子任务设置完成钩子和异常钩子的简单示例:

public class CompletableFutureDemo1 {public static void whenCompleteDemo() throws Exception {//创建异步任务CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {//模拟执行一秒Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName()+":抛出异常");throw new RuntimeException(Thread.currentThread().getName()+":发生异常");});//设置异步任务执行完成后的回调钩子future.whenComplete(new BiConsumer<Void, Throwable>() {@Overridepublic void accept(Void unused, Throwable throwable) {System.out.println(Thread.currentThread().getName()+":执行完成!");}});//设置异步任务发生异常后的回调钩子future.exceptionally(new Function<Throwable, Void>() {@Overridepublic Void apply(Throwable throwable) {System.out.println(Thread.currentThread().getName()+":执行失败!" + throwable.getMessage());return null;}});//获取异步任务的结果//回调的触发来源异步任务结果的获取,就算异步任务内存在异常,但是没有获取异步任务的结果也不会回调异常钩子或者抛出异常future.get();}public static void main(String[] args) throws Exception {whenCompleteDemo();}
}

调用cancel()方法取消CompletableFuture时,任务被视为异常完成,completeExceptionally()方法所设置的异常回调钩子也会被执行。

如果没有设置异常回调钩子,发生内部异常时会有两种情况发生:

(1)在调用get()和get(long,TimeUnit)方法启动任务时,如果遇到内部异常,get()方法就会抛出ExecutionException(执行异常)。

(2)在调用join()和getNow(T)启动任务时(大多数情况下都是如此),如果遇到内部异常,join()和getNow(T)方法就会抛出CompletionException。

5.调用handle()方法统一处理异常和结果

除了分别通过whenComplete、exceptionally设置完成钩子、异常钩子之外,还可以调用handle()方法统一处理结果和异常。

handle方法有三个重载版本如下:
 

//在执行任务的同一个线程中处理异常和结果
public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
//可能不在执行任务的同一个线程中处理异常和结果
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
//在指定线程池executor中处理异常和结果
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)

handle()方法的示例代码如下:

public class CompletableDemo2 {public static void handleDemo() throws Exception {CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {try {//模拟执行1秒Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName()+":抛出异常");throw new RuntimeException(Thread.currentThread().getName()+":发生异常");});//统一处理异常和结果future.handle(new BiFunction<Void, Throwable, Void>() {@Overridepublic Void apply(Void unused, Throwable throwable) {if (throwable == null) {System.out.println(Thread.currentThread().getName()+":没有发生异常!");} else {System.out.println(Thread.currentThread().getName()+":sorry,发生了异常!");}return null;}});future.get();}public static void main(String[] args) throws Exception {handleDemo();}
}
6.异步任务的串行执行

如果两个异步任务需要串行(一个任务依赖另一个任务)执行,可以通过CompletionStage接口的thenApply()、thenAccept()、thenRun()和thenCompose()四个方法来实现。

thenApply()
//后一个任务与前一个任务在同一个线程中执行
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) 
//后一个任务与前一个任务不在同一个线程中执行
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) 
//后一个任务在指定的executor线程池中执行
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)

thenApply的三个重载版本有一个共同的参数fn,该参数表示要串行执行的第二个异步任务,它的类型为Function。fn的类型声明涉及两个泛型参数,具体如下:

  • 泛型参数T:上一个任务所返回结果的类型。
  • 泛型参数U:当前任务的返回值类型。

调用thenApply分两步计算(10+10)*2:

//调用thenApply分两步计算(10+10)*2
public class ThenApplyDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {@Overridepublic Long get() {long firstStep = 10L + 10L;System.out.println(Thread.currentThread().getName()+":firstStep out is " + firstStep);return firstStep;}}).thenApplyAsync(new Function<Long, Long>() {@Overridepublic Long apply(Long firstStepOut) {long secondStep = firstStepOut * 2;System.out.println(Thread.currentThread().getName()+":secondStep out is " + secondStep);return secondStep;}});Long result = future.get();System.out.println(Thread.currentThread().getName()+":out is " + result);}
}
thenRun()方法

thenRun()方法与thenApply()方法不同的是,不关心任务的处理结果。只要前一个任务执行完成,就开始执行后一个串行任务。

//后一个任务与前一个任务在同一个线程中执行
public CompletableFuture<Void> thenRun(Runnable action)
//后一个任务与前一个任务不在同一个线程中执行
public CompletableFuture<Void> thenRunAsync(Runnable action)
//后一个任务在指定的executor线程池中执行
public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor) 

从方法的声明可以看出,thenRun()方法同thenApply()方法类似,不同的是前一个任务处理完成后,thenRun()并不会把计算的结果传给后一个任务,而且后一个任务也没有结果输出。

thenAccept()方法

调用thenAccept()方法时后一个任务可以接收(或消费)前一个任务的处理结果,但是后一个任务没有结果输出。

//后一个任务与前一个任务在同一个线程中执行
public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
//后一个任务与前一个任务不在同一个线程中执行
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) 
//后一个任务在指定的executor线程池中执行
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor)
thenCompose()方法

thenCompose方法在功能上与thenApply()、thenAccept()、thenRun()一样,可以对两个任务进行串行的调度操作,第一个任务操作完成时,将它的结果作为参数传递给第二个任务。

public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) 
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn,Executor executor) 

thenCompose()方法要求第二个任务的返回值是一个CompletionStage异步实例。因此,可以调用CompletableFuture.supplyAsync()方法将第二个任务所要调用的普通异步方法包装成一个CompletionStage实例。

这里使用thenCompose()分两步计算(10+10)*2

public class ThenComposeDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {@Overridepublic Long get() {long firstStep = 10 + 10;System.out.println(Thread.currentThread().getName()+":first out is " + firstStep);return firstStep;}}).thenCompose(new Function<Long, CompletionStage<Long>>() {@Overridepublic CompletionStage<Long> apply(Long firstStepOut) {//将第二个任务所要调用的普通异步方法包装成一个CompletionState异步实例return CompletableFuture.supplyAsync(new Supplier<Long>() {//两个任务所要调用的普通异步方法@Overridepublic Long get() {long secondStep = firstStepOut * 2;System.out.println(Thread.currentThread().getName()+":second Step out is " + secondStep);return secondStep;}});}});Long result = future.get();System.out.println(Thread.currentThread().getName()+":out is " + result);}
}
四个任务串行方法的区别

thenApply()、thenRun()、thenAccept()这三个方法的不同之处主要在于其核心参数fn、action、consumer的类型不同,分别为Function<T,R>、Runnable、Consumer<? super T>类型。

但是,thenCompose()方法与thenApply()方法有本质的不同:

  • thenCompose()的返回值是一个新的CompletionStage实例,可以持续用来进行下一轮CompletionStage任务的调度。
  • thenApply()的返回值就是第二个任务的普通异步方法的执行结果,它的返回类型与第二不执行的普通异步方法的返回类型相同,通过thenApply()所返回的值不能进行下一轮CompletionStage链式(或者流式)调用。
7.异步任务的合并执行

如果某个任务同时依赖另外两个异步任务的执行结果,就需要对另外两个异步任务进行合并。以泡茶为例,“泡茶喝”任务需要对“烧水”任务与“清洗”任务进行合并。

thenCombine()方法
thenCombine()会在两个CompletionStage任务都执行完成后,把两个任务的结果一起交给thenCombine()来处理。
//合并代表第二步任务(参数other)的CompletionStage实例,返回第三步任务的CompletionStage
public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)
//不一定在同一个线程中执行第三步任务的CompletionStage实例
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)
//第三步任务的CompletionStage实例在指定的executor线程池中执行
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn, Executor executor)

thenCombine()方法的调用者为第一步的CompletionStage实例,该方法的第一个参数为第二步的CompletionStage实例,该方法的返回值为第三步的CompletioStage实例。在逻辑上,thenCombine()方法的功能是将第一步、第二步的结果合并到第三步上。

thenCombine()系列方法有两个核心参数:

  • other参数:表示待合并的第二步任务的CompletionStage实例。
  • fn参数:表示第一个任务和第二个任务执行完成后,第三步需要执行的逻辑。

fn参数的类型为BiFunction<? super T,? super U,? extends V>,该类型的声明涉及三个泛型参数:

  • T:表示第一个任务所返回结果的类型
  • U:表示第二个任务所返回结果的类型
  • V:表示第三个任务所返回结果的类型
//使用thenCombine()计算(10+10)*(10+10)
public class ThenCombineDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {Integer firstStep = 10 + 10;System.out.println(Thread.currentThread().getName()+":firstStep out is " + firstStep);return firstStep;}});CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {@Overridepublic Integer get() {Integer secondStep = 10 + 10;System.out.println(Thread.currentThread().getName()+":secondStep out is " + secondStep);return secondStep;}});CompletableFuture<Integer> future3 = future1.thenCombine(future2, new BiFunction<Integer, Integer, Integer>() {@Overridepublic Integer apply(Integer step1Out, Integer step2Out) {return step1Out * step2Out;}});Integer result = future3.get();System.out.println(Thread.currentThread().getName()+":out is " + result);}
}
runAfterBoth()方法

runAfterBoth()方法不关心每一步任务的输入参数和处理结果。

//合并第二步任务的CompletionStage实例,返回第三步任务的CompletionStage
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other,Runnable action)
//不一定在同一个线程中执行第三步任务的CompletionStage实例
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action)
//第三步任务的CompletionStage实例在指定的executor线程池中执行
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor) 
thenAcceptBoth()方法

该方法是对runAfterBoth()方法和thenCombine()方法的特点进行了折中,调用thenAcceptBoth()方法,第三个任务可以接收其合并过来的第一个任务、第二个任务的处理结果,但是第三个任务(合并任务)却不能返回结果。

public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action)
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action)
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor)
allOf()等待所有的任务结束

CompletionStage接口的allOf()会等待所有的任务结束,以合并所有的任务。thenCombine()只能合并两个任务,如果需要合并多个异步任务,那么可以调用allOf()。阻塞所有任务执行完成之后,才可以往下执行。

//allOF()会等待所有的任务结束,以合并所有的任务
public class AllOfDemo {public static void main(String[] args) {CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName()+":模拟异步任务1"));CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName()+":模拟异步任务2"));CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName()+":模拟异步任务3"));CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName()+":模拟异步任务4"));CompletableFuture<Void> all = CompletableFuture.allOf(future1, future2, future3, future4);all.join();System.out.println(Thread.currentThread().getName()+":所有异步任务都已经执行完毕");}
}


 

8.采摘果园实例

为了异步执行整个排查流程,分别设计三个线程:运输果实线程(MainThread,主线程)、采苹果线程(HotWaterThread)和采橘子线程(WashThread)。

  • 运输果实线程的工作是:启动采苹果线程、启动采橘子线程,等采苹果、采橘子的工作完成后,运输到仓库;
  • 采苹果线程的工作是:找到苹果、采苹果、放到框里;
  • 采橘子线程的工作是:找到橘子、采橘子、放到框里;

我们使用CompletableFuture实现整个采摘果园程序。我们分3个任务:

任务1负责采苹果

任务2负责采橘子

任务3负责运输


public class DrinkTea {public static final int SLEEP_GAP = 3000;public static void main(String[] args) throws ExecutionException, InterruptedException {//任务1:采苹果CompletableFuture<Boolean> hotJob = CompletableFuture.supplyAsync(() -> {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":找到苹果"+i);System.out.println(Thread.currentThread().getName() + ":采苹果"+i);System.out.println(Thread.currentThread().getName() + ":框里"+i+"苹果");}//线程睡眠一段时间,代表采苹果中try {Thread.sleep(SLEEP_GAP);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + ":框满了");return true;});//任务2:采橘子CompletableFuture<Boolean> washJob = CompletableFuture.supplyAsync(() -> {for (int i = 1; i <= 10; i++) {System.out.println(Thread.currentThread().getName() + ":找到橘子"+i);System.out.println(Thread.currentThread().getName() + ":采橘子"+i);System.out.println(Thread.currentThread().getName() + ":框里有"+i+"橘子");}//线程睡眠一段时间,代表采橘子中try {Thread.sleep(SLEEP_GAP);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + ":框满了");return true;});//任务3:任务1和任务2完成后执行运输CompletableFuture<String> drinkJob = hotJob.thenCombine(washJob, new BiFunction<Boolean, Boolean, String>() {@Overridepublic String apply(Boolean hotOK, Boolean washOK) {if (hotOK && washOK) {System.out.println(Thread.currentThread().getName() + ":苹果框满了,橘子框满了。开始运输");return "运输完成";}return "苹果框或者橘子框未满";}});//等待任务3执行结果System.out.println(Thread.currentThread().getName() + ":" + drinkJob.get());}
}



 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_926290.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux-ARM裸机(十一)-UART串口通信

无论单片机开发还是嵌入式 Linux 开发&#xff0c;串口都是最常用到的外设。可通过串口将开发板与电脑相连&#xff0c;然后在电脑上通过串口调试助手来调试程序。还有很多的模块&#xff0c;比如蓝牙、GPS、 GPRS 等都使用的串口来与主控进行通信的&#xff0c;在嵌入式 Linux…

基于信号完整性的PCB设计原则

最小化单根信号线质量的一些PCB设计建议 1. 使用受控阻抗线&#xff1b; 2. 理想情况下&#xff0c;所有信号都应该使用完整的电源或地平面作为其返回路径&#xff0c;关键信号则使用地平面作为返回路径&#xff1b; 3. 信号的返回参考面发生变化时&#xff0c;在尽可能接近…

wins安装paddle框架

一、安装 https://www.paddlepaddle.org.cn/install/quick?docurl/documentation/docs/zh/install/pip/windows-pip.html 装包&#xff08;python 的版本是否满足要求&#xff1a; 3.8/3.9/3.10/3.11/3.12&#xff0c; pip 版本为 20.2.2 或更高版本 &#xff09; CPU 版:…

LLM论文:ALCE (Enabling Large Language Models to Generate Text with Citations)

这是一篇RAG领域的文章&#xff0c;原文在这&#xff1a;https://aclanthology.org/2023.emnlp-main.398.pdf 时间[Submitted on 24 May 2023 (v1), last revised 31 Oct 2023 (this version, v2)]背景LLM在信息搜索、生成带引用的文本时存在幻觉问题&#xff0c;即事实准确性…

《 乱弹篇(四)》

既然是“乱弹”&#xff0c;弹&#xff08;谈&#xff09;题便可以包罗万象&#xff1b;天上地下&#xff0c;飞的走的&#xff0c;你的我的他的事儿&#xff0c;甚至还有许许多多八竿子都打不着的怪涎事儿&#xff0c;都可成为弹&#xff08;谈&#xff09;资 。比如&#xff…

计算机毕业设计 | 大型SpringBoot宠物医院管理 宠物商城购物系统(附源码)

写在前面 Le Dao宠物医院管理系统是一个超大型的&#xff0c;完成度很高的&#xff0c;集宠物医疗、宠物美容、宠物交易、宠物周边等各种功能于一身的&#xff0c;权限涵盖普通用户、医生、化验师、美容师、仓库主管、采购员等多种角色于一体的大型宠物医疗&#xff0c;购物系…

Rust-Panic

什么是panic 在Rust中&#xff0c;有一类错误叫作panic。示例如下&#xff1a; 编译&#xff0c;没有错误&#xff0c;执行这段程序&#xff0c;输出为&#xff1a; 这种情况就引发了一个panic。在这段代码中&#xff0c;我们调用了Option::unwrap()方法&#xff0c;正是这个方…

【开源】基于JAVA语言的网上药店系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 药品类型模块2.3 药品档案模块2.4 药品订单模块2.5 药品收藏模块2.6 药品资讯模块 三、系统设计3.1 用例设计3.2 数据库设计3.2.1 角色表3.2.2 药品表3.2.3 药品订单表3.2.4 药品收藏表3.2.5 药品留言表…

【Django开发】美多商城项目第2篇:Django用户注册和登录开发(附代码,已分享)

本系列文章md笔记&#xff08;已分享&#xff09;主要讨论django商城项目相关知识。项目利用Django框架开发一套前后端不分离的商城项目&#xff08;4.0版本&#xff09;含代码和文档。功能包括前后端不分离&#xff0c;方便SEO。采用Django Jinja2模板引擎 Vue.js实现前后端…

Kubernetes (K8S) 3 小时快速上手 + 实践

1. Kubernetes 简介 k8s即Kubernetes。其为google开发来被用于容器管理的开源应用程序&#xff0c;可帮助创建和管理应用程序的容器化。用一个的例子来描述&#xff1a;"当虚拟化容器Docker有太多要管理的时候&#xff0c;手动管理就会很麻烦&#xff0c;于是我们便可以通…

【SQL注入】SQLMAP v1.7.11.1 汉化版

下载链接 【SQL注入】SQLMAP v1.7.11.1 汉化版 简介 SQLMAP是一款开源的自动化SQL注入工具&#xff0c;用于扫描和利用Web应用程序中的SQL注入漏洞。它在安全测试领域被广泛应用&#xff0c;可用于检测和利用SQL注入漏洞&#xff0c;以验证应用程序的安全性。 SQL注入是一种…

初识OpenCV

首先你得保证你的虚拟机Ubuntu能上网 可看 http://t.csdnimg.cn/bZs6c 打开终端输入 sudo apt-get install libopencv-dev 回车 输入密码 回车 遇到Y/N 回车 OpenCV在线文档 opencv 文档链接 点zip可以下载&#xff0c;点前面的直接在线浏览&#xff0c;但是很慢 https…

k8s云原生环境搭建笔记——第二篇

目录 1、使用普通方式安装prometheus和grafana1.1、安装kube-state-metrics容器1.1.1、下载并修改yaml文件1.1.2、导入kube-state-metrics镜像1.1.3、执行yaml文件目录 1.2、安装node-exploer1.2.1、创建名称空间prometheus1.2.2、执行yaml 1.3、安装prometheus1.3.1、创建集群…

基于python集成学习算法XGBoost农业数据可视化分析预测系统

文章目录 基于python集成学习算法XGBoost农业数据可视化分析预测系统一、项目简介二、开发环境三、项目技术四、功能结构五、功能实现模型构建封装类用于网格调参训练模型系统可视化数据请求接口模型评分 0.5*mse 六、系统实现七、总结 基于python集成学习算法XGBoost农业数据可…

多行SQL转成单行SQL

如下图所示 将以上多行SQL转成单行SQL 正则表达式如下 (?s)$[^a-zA-Z()0-9]*结果如下 灵活使用,也未必只能使用Sublime Text 提供了一个在线工具

[Docker] Docker为什么出现

Docker为什么出现 一款产品&#xff1a; 开发–上线 -->两套环境 | 应用配置 开发即运维&#xff01; 环境配置十分麻烦&#xff0c;每一个机器都要部署环境&#xff08;Redis, ES, Hadoop&#xff09; 费时费力 项目带上配置环境安装打包。 传统&#xff1a; 开发jar&…

基于pyqt5+scapy 根据ip 具体端口 进行扫描 的程序

先给出代码 import sysfrom PyQt5 import uic from PyQt5.QtWidgets import *from scapy.all import * import argparse import logging from scapy.layers.inet import IP, TCP from scapy.sendrecv import sr1class MyWindow(QWidget):def __init__(self):super().__init__(…

AI人工智能工程师证书专业认证培训班有用吗?

当然有用&#xff0c;它即让自身技术技能有所提升&#xff0c;也拿到行内有含金量的证书&#xff0c;让自己在选择职业、升职加薪中更有竞争力。但是要擦亮眼睛&#xff0c;建议大家如果要找人工智能培训&#xff0c;就找性价比较高的培训班&#xff0c; 人工智能AI培训班怎么…

IP风险画像:源头防范网络攻击的全面策略

在当今数字化的时代&#xff0c;网络攻击呈现多样化和复杂化的趋势&#xff0c;为了确保网络的安全&#xff0c;制定全面的IP风险画像并从源头防范网络攻击是至关重要的。ip数据云将探讨如何通过建立IP风险画像来识别和应对潜在的威胁&#xff0c;从而实现更加安全可靠的网络环…

UML-用例图

提示&#xff1a;用例图是软件建模的开始&#xff0c;软件建模中的其他图形都将以用例图为依据。用例图列举了系统所需要实现的所有功能&#xff0c;除了用于软件开发的需求分析阶段&#xff0c;也可用于软件的系统测试阶段。 UML-用例图 一、用例图的基础知识1.用例图的构成元…