L
L
loxnemamont2016-08-27 13:12:05
Python
loxnemamont, 2016-08-27 13:12:05

How to write to the same file from different threads at the same time in python?

Hello! I'm just starting to learn Python, I decided to write a simple checker - the script goes through the list of sites and checks for the occurrence of the specified substring in the page code. It all looks like this.

def get_html(url):
  try:
    response = urllib.request.urlopen(url, timeout=5)
  except Exception:
    return 'error'
  return str(response.read())

def check(html):
  if 'customtext' in html:
    return True
  else:
    return False

I decided to fasten multithreading according to this manual - toly.github.io/blog/2014/02/13/parallelism-in-one-line but I just can’t figure out how to implement writing results to 1 file from different streams. For example, if check() returns True, the original domain is written to the good.txt file, if False - to bad.txt.
Thanks in advance for any advice!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2016-08-27
@dimonchik2013

well, if "to a file", then open, append, close
in my opinion, you can also open it for appending at the same time,
and serious guys use the logging module

A
Andrey K, 2016-08-29
@mututunus

Why do you need multithreading here? Asyncio is perfect for this task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question