Python run()関数とstart()関数の比較と違いを紹介します。


run()メソッドは新しいスレッドを起動しません。メインスレッドで一般関数を呼び出しただけです。
start()は、スレッド名を自分で定義したnameとして起動する方法です。
したがって、マルチスレッドを起動するには、start()メソッドを使用する必要があります。
実例を見てください。(ソースコード)
1はrun()メソッドを使ってスレッドを起動し、それがプリントされたスレッド名はMainThread、つまりメインスレッドです。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(1)
count += 1
print(“thread name = {}”.format(threading.current_thread().name))

print(“Start Test run()”)
t1 = threading.Thread(target=worker, name=“MyTryThread”)
t1.run()

print(“run() test end”)
実行結果:

Start Test run()
thread name = MainThread
thread name = MainThread
thread name = MainThread
run() test end
2 start()メソッドを使って起動するスレッド名は、スレッドオブジェクトを定義する際に設定したname="MyThread"の値であり、nameパラメータ値が設定されていないと、システムに割り当てられたThread-1,Thread-2という名前が印刷されます。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}”.format(threading.current_thread().name)) #      

print(“Start Test start()”)
t = threading.Thread(target=worker, name=“MyTryThread”)
t.start()
t.join()

print(“start() test end”)
実行結果:

Start Test start()
thread name = MyTryThread
thread name = MyTryThread
thread name = MyTryThread
start() test end
3つのサブスレッドはいずれもrun()で起動されますが、t 1.run()を先に実行して、t 2.run()を順次実行します。両スレッドはメインスレッドで動作しています。新しいスレッドが起動されていません。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}, thread id = {}”.format(threading.current_thread().name,
threading.current_thread().ident))

print(“Start Test run()”)
t1 = threading.Thread(target=worker, name=“t1”)
t2 = threading.Thread(target=worker, name=‘t2')

t1.run()
t2.run()

print(“run() test end”)
実行結果:

Start Test run()
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
run() test end
4 start()を使用して2つの新しいサブスレッドを起動して交互に実行します。各サブプロセスIDも違います。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}, thread id = {}”.format(threading.current_thread().name,
threading.current_thread().ident))

print(“Start Test start()”)
t1 = threading.Thread(target=worker, name=“MyTryThread1”)
t2 = threading.Thread(target=worker, name=“MyTryThread2”)
t1.start()
t2.start()
t1.join()
t2.join()
print(“start() test end”)
実行結果:

Start Test start()
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
start() test end
知識を補充します:pythonファイルの操作はいつも車輪を使います。
パス
注意:ファイル名を処理する必要がある問題については、文字列ではなくOS.パスモジュールを使用しなければなりません。二つの原因は、OS.pathがwindows、linuxなどの移植問題を処理することができます。もう一つの理由は、車輪を繰り返さないことです。
ファイル名を取得

import os
filename = os.path.basename(filepath)
print(filename)
ファイルの現在のフォルダフォルダを取得します。
filename=os.path.dirname(filepath)
フォルダとファイル名を同時に取得します。
dirname,filename=os.path.split(filepath)
スプリットファイル拡張子

path_without_ext, ext = os.path.splitext(filepath)
# e.g 'hello/world/read.txt' then
# path_without_ext = hello/world/read, ext = .txt
フォルダを巡回するすべてのファイル方法
import glob
pyfiles=glob.glob('*.py')
or

def getAllFiles(filePath, filelist=[]):
  for root, dirs, files in os.walk(filePath):
    for f in files:
      filelist.append(os.path.join(root, f))
      print(f)
  return filelist
ファイルfileかどうかを判断します。
os.path.isfile('/etc/passwd')
フォルダのフォルダーかどうかを判断します。
os.path.isdir('/etc/passwd')
ソフトリンクかどうか
os.path.islink('/usr/local/bin/python 3')
ソフトリンクが本当に指しているのは
os.path.realpath('/usr/local/bin/python 3')
size
ファイルサイズを取得

import os
size = os.path.getsize(filepath)
print(size)
フォルダサイズを取得

import os
 
def getFileSize(filePath, size=0):
  for root, dirs, files in os.walk(filePath):
    for f in files:
      size += os.path.getsize(os.path.join(root, f))
      print(f)
  return size
 
print(getFileSize("."))
時間

import time
t1 = os.path.gettime('/etc/passwd')
# t1 1272478234.0
t2 = time.ctime(t1)
# t2 'Wed Apr 28 12:10:05 2010'
以上のPython run()関数とstart()関数の比較と違いは、小編集が皆さんに共有しているすべての内容です。参考にしていただければと思います。どうぞよろしくお願いします。