-2

Script below is using python threads:

import logging
import threading
import time

def thread_function(name):
    logging.info("Thread %s: starting", name)
    time.sleep(2)
    logging.info("Thread %s: finishing", name)


if ____name____ == "____main____":

    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO,
                        datefmt="%H:%M:%S")
    logging.info("Main    : before creating thread")
    x = threading.Thread(target=thread_function, args=(1,))
    logging.info("Main    : before running thread")
    x.start()
    logging.info("Main    : wait for the thread to finish")
    # x.join()
    logging.info("Main    : all done")

Output of the script above is:

10:04:50: Main    : before creating thread
10:04:50: Main    : before running thread
10:04:50: Thread 1: starting
10:04:50: Main    : wait for the thread to finish
10:04:50: Main    : all done
10:04:52: Thread 1: finishing

While when I use multiprocessor thing:

from multiprocessing import Process
import time
import logging


def thread_function(name):
    logging.info("Thread %s: starting", name)
    time.sleep(2)
    logging.info("Thread %s: finishing", name)

if ____name____ == "____main____":

    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO,
                        datefmt="%H:%M:%S")
    logging.info("Main    : before creating thread")
    p1 = Process(target=thread_function, args=(1,))
    logging.info("Main    : before running thread")
    p1.start()
    logging.info("Main    : wait for the thread to finish")
    p1.join()
    logging.info("Main    : all done"

It's not capturing the output of the function that the process is calling as you can see in the output below:

10:08:58: Main    : before creating thread
10:08:58: Main    : before running thread
10:08:58: Main    : wait for the thread to finish
10:09:00: Main    : all done

Can anyone tell what I have missing here?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

A thread is different from a process.

Multiprocessing spawns a new process which runs independiently from your main one, while a thread is a new execution task which is bound to the parent program.

When you create a thread, your program is sharing everything with its childs (such as variables, functions, etc.) so that's why you are able to log some events from a thread. But, in a new process, you are, initially, sharing nothing with the main program but some descriptors and information (who is the parent, who is the child, etc.). In your case, when you are logging on a new process you are basically creating a new logging instance which has nothing to do with parent's one.

For daily work I recommend you to just use the threading library instead of multiprocessing one. In case you need to use the latter (for remote process execution, daemon creation, etc.) I recommend you to read the following documentation:

Hope this helps

0x2b3bfa0
  • 191
  • 6
  • 18
Javinator9889
  • 16
  • 1
  • 2