Upload New File

parent 93a8f3cd
import threading
from datetime import datetime
import time
class Logger:
_instance = None
def __new__(cls):
if cls._instance is None:
time.sleep(0.01)
cls._instance = super(Logger, cls).__new__(cls)
cls._instance._initialize()
return cls._instance
def _initialize(self):
self.log_file = open("unsafe.log", "a")
def log(self, message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.log_file.write(f"[{timestamp}] {message}\n")
def create_instance(thread_id):
logger = Logger()
logger.log(f"Message#{thread_id}")
if __name__ == "__main__":
threads = [threading.Thread(target=create_instance, args=(i,)) for i in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
print("Готово. Все потоки записали лог.")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment