Giter Club home page Giter Club logo

Comments (4)

dongrixinyu avatar dongrixinyu commented on August 13, 2024

python 哪里有管理线程的需求?最好贴出来哪条数据处理出错了

from jionlp.

coolbeevip avatar coolbeevip commented on August 13, 2024

python 哪里

此处做了判断,不确定是否为了避免重复执行 self._preprocess(),不过在代码中没有发现为 self.future_time 赋值的地方

https://github.com/dongrixinyu/JioNLP/blob/master/jionlp/gadget/time_parser.py#L778

from jionlp.

coolbeevip avatar coolbeevip commented on August 13, 2024

我为此写了一个测试用例,可以复现我说的问题

from concurrent.futures import ThreadPoolExecutor
from unittest import TestCase

from jionlp import TimeParser


class TestJioNLP(TestCase):
    def setUp(self) -> None:
        self.parse_time = TimeParser()

    def test_thread_safe(self):
        with ThreadPoolExecutor(max_workers=5) as executor:
            futures = {executor.submit(self.parse_time, "2023年11月1日") for _ in range(5)}
            for future in futures:
                obj = future.result()
                self.assertEqual(obj['time'], ['2023-11-01 00:00:00', '2023-11-01 23:59:59'])

这个用例在执行的时候会得到如下错误

test_jionlp.py:8 (TestJioNLP.test_thread_safe)
self = <tests.test_jionlp.TestJioNLP testMethod=test_thread_safe>

    def test_thread_safe(self):
        with ThreadPoolExecutor(max_workers=5) as executor:
            futures = {executor.submit(self.parse_time, "2023年11月1日") for _ in range(5)}
            for future in futures:
>               obj = future.result()

test_jionlp.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../../.pyenv/versions/3.11.4/lib/python3.11/concurrent/futures/_base.py:449: in result
    return self.__get_result()
../../../../.pyenv/versions/3.11.4/lib/python3.11/concurrent/futures/_base.py:401: in __get_result
    raise self._exception
../../../../.pyenv/versions/3.11.4/lib/python3.11/concurrent/futures/thread.py:58: in run
    result = self.fn(*self.args, **self.kwargs)
../.venv/lib/python3.11/site-packages/jionlp/gadget/time_parser.py:768: in __call__
    period_res, blur_time = self.parse_time_period(time_string, period_results_num=period_results_num)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <jionlp.gadget.time_parser.TimeParser object at 0x105a40450>
time_string = '2023年11月1日', period_results_num = None

    def parse_time_period(self, time_string, period_results_num=None):
        """ 判断字符串是否为 time_period,若是则返回结果,若不是则返回 None,跳转到其它类型解析。 """
    
        has_weekday = False
        if '工作日' in time_string:
            has_weekday = True
    
>       searched_res = self.period_time_pattern.search(time_string)
E       AttributeError: 'TimeParser' object has no attribute 'period_time_pattern'

../.venv/lib/python3.11/site-packages/jionlp/gadget/time_parser.py:1169: AttributeError

Process finished with exit code 1

from jionlp.

dongrixinyu avatar dongrixinyu commented on August 13, 2024

哪能这么用,初始化是在初次调用 call 时候的。线程开启在__call__ 之前,初始化没做干净就开始解析,自然就报错了。
除非你手工执行 self.parse_time._preprocess() 函数

from jionlp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.