Skip to content

Commit

Permalink
Support pytest with threading
Browse files Browse the repository at this point in the history
  • Loading branch information
hankcs committed Jan 14, 2025
1 parent e93d79b commit 7f0870e
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions tests/test_multithread.py
Original file line number Diff line number Diff line change
@@ -14,13 +14,12 @@
__author__ = "Hai Liang Wang"
__date__ = "2018-03-23:17:18:30"

import unittest
import threading
import time
from pyhanlp import HanLP, SafeJClass

# 在线程体外部用SafeJClass线程安全地引入类名
CRFLexicalAnalyzer = SafeJClass("com.hankcs.hanlp.model.crf.CRFLexicalAnalyzer")
import pytest

from pyhanlp import SafeJClass


class MyThread(threading.Thread):
@@ -39,34 +38,32 @@ def run(self):
self.counter -= 1


class Test(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def test_multithread(self):
# 在线程外部创建对象,供多个线程共用
analyzer = CRFLexicalAnalyzer()
@pytest.fixture()
def analyzer():
# 在线程体外部用SafeJClass线程安全地引入类名
CRFLexicalAnalyzer = SafeJClass("com.hankcs.hanlp.model.crf.CRFLexicalAnalyzer")
# 在线程外部创建对象,供多个线程共用
analyzer = CRFLexicalAnalyzer()
return analyzer

thread1 = MyThread("Thread-1", 1, analyzer)
thread2 = MyThread("Thread-2", 2, analyzer)

thread1.start()
thread2.start()
@pytest.fixture()
def thread1():
return MyThread("Thread-1", 1, analyzer)

print('waiting to finish the thread')

thread1.join()
thread2.join()
@pytest.fixture()
def thread2():
return MyThread("Thread-2", 2, analyzer)

print("Exiting Main Thread")

def test_sth(thread1, thread2):
thread1.start()
thread2.start()

def main():
unittest.main()
print('waiting to finish the thread')

thread1.join(timeout=3)
thread2.join(timeout=3)

if __name__ == '__main__':
main()
print("Exiting Main Thread")

0 comments on commit 7f0870e

Please sign in to comment.