Use 'transformers.BertModel.from_pretrained', The code is blocked
Author: yangh0597
Pull request: no

![52ae2d1edf2fa3044e6932d42c558f1](https://github.com/huggingface/transformers/assets/86940083/180c1033-375a-46b8-af7e-cda344e1e5ff)


this is py-spy result:
![image](https://github.com/huggingface/transformers/assets/86940083/5d5aa094-fa16-452d-ab39-8700fa4d8d1e)


Comments:
Hi, are you running the script/command in some particular setting?

Looks like it's in a multiprocessing setting? Could you provide a self-complete code snippet instead of just uploading screenshot? Thanks in advance.

if  not use pyrocketmq is ok. but use pyrocketmq  not ok. the code is:
```
import jpype.imports

jpype.startJVM(classpath=['D:\\soft\\rocketmq-all-4.3.2-bin-release\\lib\\*', ])
from pyrocketmq import *

# import json
# from pyrocketmq.common.message import Message
# from pyrocketmq.client.producer import Producer, SendStatus
# pr = Producer('test_producer')
# pr.setNamesrvAddr('10.2.10.6:9876')
# pr.start()
# body = json.dumps({'name':'Alice', 'age':1}).encode('utf-8')
# msg = Message(topic='test_topic', body=body, tags='girl')
# # send, tcp-like, return sendStatus
# sr = pr.send(msg)
# assert(sr.sendStatus == SendStatus.SEND_OK)
# pr.shutdown()




from multiprocessing import Pool
import json
import time
from typing import List
from pyrocketmq.client.consumer.listener import ConsumeConcurrentlyContext, ConsumeConcurrentlyStatus, MessageListenerConcurrently
from pyrocketmq.client.consumer.consumer import MessageSelector, PushConsumer
from pyrocketmq.common.common import ConsumeFromWhere
from pyrocketmq.common.message import MessageExt


def from_pretrained():
    print('--from_pretrained1--')
    transformers.BertModel.from_pretrained('/opt/model-service/volume/resource/bert_base')
    print('--from_pretrained2--')

    return True


# subclass MessageListenerConcurrently to write your own consume action
class MyMessageListenerConcurrently(MessageListenerConcurrently):
    def _consumeMessage(self, msgs:List[MessageExt], context:ConsumeConcurrentlyContext) -> ConsumeConcurrentlyStatus:
        print('Concurrently', context.ackIndex)
        for msg in msgs:
            print(msg.body)
        print('--_main--')
        pool = Pool(processes=2)
        bert_res_future = pool.apply_async(func=from_pretrained)
        res = bert_res_future.get()
        print(res)
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS

cs = PushConsumer('test_push_consumer')
cs.setNamesrvAddr('10.2.10.6:9876')
selector = MessageSelector.byTag('model')
ml = MyMessageListenerConcurrently()
cs.registerMessageListener(ml)
cs.subscribe('test_topic', selector)
cs.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET)
cs.start()
```


The code below is problematic, the code above is not
```
import transformers


def from_pretrained():
    print('--from_pretrained1--')

    transformers.BertModel.from_pretrained('/opt/model-service/volume/resource/bert_base')
    print('--from_pretrained2--')

    return True





if __name__ == '__main__':
    from multiprocessing import Pool

    print('--_main--')
    pool = Pool(processes=2)
    bert_res_future = pool.apply_async(func=from_pretrained)
    res=bert_res_future.get()
    print(res)

```



Thanks for clarification @yangh0597, appreciated. This is more `pyrocketmq` issue (or the way it works) rather than `transformers`.

In general, when doing such multiprocessing thing or inter-communication stuff between processes, we should not pass large objects (inputs, models) etc., but rather creating the necessary objects in the target process(es). It's on the users to take care what would be necessary steps to avoid the blocking.

We wouldn't be able to help with the details, especially it involves 3rd party library `pyrocketmq`. But I hope the above comment give you some hint(s) to figure out a working solution.

thakns very much

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

done