生成器close方法结束生成器迭代进行,多线程中有的close有的不close

使用close() 方法结束生成器。

1

2

3

4

5

6

7

上面例子中f.close()则关闭了生成器。

>>> f.close()

>>> next(f)

Traceback (most recent call last):

  File "<pyshell#92>", line 1in <module>

    next(f)

StopIteration

 

举例:一个线程正在从生成器中依次生成元素,处理某一事务, 另一线程 可以通过send函数发送指令或数据给生成器,从而影响原来使用生成器线程的事务。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

import threading

import time

class Thread1(threading.Thread):

    def __init__(self, name, fib):

        threading.Thread.__init__(self)

        self.name = name

        self.fib = fib

 

    def run(self):

        for in range(10):

            try:

                time.sleep(1)

                print(next(self.fib))

            except StopIteration:

                print("over")

                break            

 

 

class Thread2(threading.Thread):

    def __init__(self, name, fib):

        threading.Thread.__init__(self)

        self.name = name

        self.fib = fib

 

    def run(self):

        time.sleep(3)

        try:

            self.fib.send("quit")

            # self.fib.close()        

        except StopIteration:

            print('结束生成器')

 

def Fib():

    a,b =0,1    while True:

        = yield b

        if == 'quit':

            break        

        a,b = b,a+b

 

= Fib()

 

t1 = Thread1('thread1',a)

t2 = Thread2('thread2',a)

 

t1.start()

t2.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你使用 `@Component` 注解将自定义的 Mybatis 拦截器注入到 Spring 容器,并且在拦截器调用了 `Executor` 接口的 `close` 方法,但是出现了找不到该方法的情况,这可能是因为 `Executor` 接口的 `close` 方法是在 Mybatis 3.5.0 版本新增的,而 Spring Boot 默认集成的 Mybatis 版本比较低。 但是,即使在较低的版本,`Executor` 接口下确实有 `close` 方法。因此,如果你的代码出现了找不到 `Executor` 接口下的 `close` 方法的情况,可能是因为你的 `Executor` 对象的实际类型不是 Mybatis 的 `SimpleExecutor` 或 `ReuseExecutor`。 在 Mybatis ,`SimpleExecutor` 和 `ReuseExecutor` 实现了 `Executor` 接口,并且都提供了 `close` 方法。因此,如果你的 `Executor` 对象的实际类型是 `SimpleExecutor` 或 `ReuseExecutor`,则可以直接调用 `close` 方法。 以下是一个示例代码: ``` @Component public class MyInterceptor implements Interceptor { @Autowired private SqlSessionFactory sqlSessionFactory; @Override public Object intercept(Invocation invocation) throws Throwable { Executor executor = sqlSessionFactory.openSession().getExecutor(); if (executor instanceof SimpleExecutor || executor instanceof ReuseExecutor) { executor.close(); } // do something return invocation.proceed(); } // 其他方法 } ``` 在上面的代码,我们首先获取了一个 `Executor` 对象,然后判断它的实际类型是否为 `SimpleExecutor` 或 `ReuseExecutor`,如果是,则直接调用 `close` 方法。 注意,在使用自定义拦截器时,需要在 Mybatis 的配置文件将拦截器添加到 `interceptor` 标签,并且将其放在 Mybatis 内置的拦截器之前,例如: ``` <configuration> <settings> <!-- 其他设置 --> </settings> <typeAliases> <!-- 类型别名 --> </typeAliases> <plugins> <plugin interceptor="com.example.MyInterceptor"> <!-- 自定义配置 --> </plugin> <!-- 其他插件 --> </plugins> </configuration> ``` 需要将 `com.example.MyInterceptor` 替换为你自己的拦截器类路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值