Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ import os
|
|
| 6 |
from math import isqrt
|
| 7 |
|
| 8 |
# Path to save the primes
|
| 9 |
-
PRIME_SAVE_PATH = "
|
|
|
|
| 10 |
|
| 11 |
# Function to check if a number is prime
|
| 12 |
def is_prime(n):
|
|
@@ -35,8 +36,11 @@ def load_saved_primes():
|
|
| 35 |
|
| 36 |
# Function to save new primes to file
|
| 37 |
def save_primes_to_file(prime_list):
|
| 38 |
-
with
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Background prime number generator function
|
| 42 |
def prime_generator():
|
|
@@ -58,13 +62,16 @@ def prime_generator():
|
|
| 58 |
processes.append(process)
|
| 59 |
|
| 60 |
# Collect primes from all processes
|
|
|
|
| 61 |
for process in processes:
|
| 62 |
process.join()
|
| 63 |
primes = queue.get()
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Save the newly found primes to the file
|
| 67 |
-
save_primes_to_file(
|
| 68 |
|
| 69 |
num += num_processes * batch_size
|
| 70 |
time.sleep(0.1) # Adjust this to control responsiveness and resource usage
|
|
|
|
| 6 |
from math import isqrt
|
| 7 |
|
| 8 |
# Path to save the primes
|
| 9 |
+
PRIME_SAVE_PATH = "primes.txt"
|
| 10 |
+
lock = threading.Lock() # Thread lock to control file access
|
| 11 |
|
| 12 |
# Function to check if a number is prime
|
| 13 |
def is_prime(n):
|
|
|
|
| 36 |
|
| 37 |
# Function to save new primes to file
|
| 38 |
def save_primes_to_file(prime_list):
|
| 39 |
+
with lock: # Ensure only one process writes to the file at a time
|
| 40 |
+
with open(PRIME_SAVE_PATH, "a") as file:
|
| 41 |
+
file.write("\n".join(prime_list) + "\n")
|
| 42 |
+
file.flush() # Ensure data is written to disk
|
| 43 |
+
os.fsync(file.fileno()) # Force file system to sync
|
| 44 |
|
| 45 |
# Background prime number generator function
|
| 46 |
def prime_generator():
|
|
|
|
| 62 |
processes.append(process)
|
| 63 |
|
| 64 |
# Collect primes from all processes
|
| 65 |
+
all_new_primes = []
|
| 66 |
for process in processes:
|
| 67 |
process.join()
|
| 68 |
primes = queue.get()
|
| 69 |
+
all_new_primes.extend(primes)
|
| 70 |
+
|
| 71 |
+
prime_list.extend(all_new_primes)
|
| 72 |
|
| 73 |
# Save the newly found primes to the file
|
| 74 |
+
save_primes_to_file(all_new_primes)
|
| 75 |
|
| 76 |
num += num_processes * batch_size
|
| 77 |
time.sleep(0.1) # Adjust this to control responsiveness and resource usage
|