How can I use threading in Python?

前端 未结 19 2754
迷失自我
迷失自我 2020-11-21 04:54

I am trying to understand threading in Python. I\'ve looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I\'m having trou

19条回答
  •  旧时难觅i
    2020-11-21 05:19

    Here is the very simple example of CSV import using threading. (Library inclusion may differ for different purpose.)

    Helper Functions:

    from threading import Thread
    from project import app
    import csv
    
    
    def import_handler(csv_file_name):
        thr = Thread(target=dump_async_csv_data, args=[csv_file_name])
        thr.start()
    
    def dump_async_csv_data(csv_file_name):
        with app.app_context():
            with open(csv_file_name) as File:
                reader = csv.DictReader(File)
                for row in reader:
                    # DB operation/query
    

    Driver Function:

    import_handler(csv_file_name)
    

提交回复
热议问题