How create threads under Python for Delphi

I'm hosting Python script with Python for Delphi components inside my Delphi application. I'd like to create background tasks which keep running by script.

Is it possible to create threads which keep running even if the script execution ends (but not the host process, which keeps going on). I've noticed that the program gets stuck if the executing script ends and there is thread running. However if I'll wait until the thread is finished everything goes fine.

I'm trying to use "threading" standard module for threads.

Asked by: Anna513 | Posted: 27-01-2022

Answer 1

Python has its own threading module that comes standard, if it helps. You can create thread objects using the threading module.

threading Documentation

thread Documentation

The thread module offers low level threading and synchronization using simple Lock objects.

Again, not sure if this helps since you're using Python under a Delphi environment.

Answered by: Kelvin556 | Posted: 28-02-2022

Answer 2

If a process dies all it's threads die with it, so a solution might be a separate process.

See if creating a xmlrpc server might help you, that is a simple solution for interprocess communication.

Answered by: Daryl964 | Posted: 28-02-2022

Answer 3

Threads by definition are part of the same process. If you want them to keep running, they need to be forked off into a new process; see os.fork() and friends.

You'll probably want the new process to end (via exit() or the like) immediately after spawning the script.

Answered by: Edgar542 | Posted: 28-02-2022

Similar questions

python - For my app, how many threads would be optimal?

I have a simple Python web crawler. It uses SQLite to store its output and also to keep a queue. I want to make the crawler multi-threaded so that it can crawl several pages at a time. I figured i would make a thread and just run several instances of the class at once, so they all run concurrently. But the question is, how many should i run at once? should i stick to two? can i go higher? what would be a reasonable limit f...

python - How to work with threads in pygtk

I have a problem with threads in pygtk. My application consist of a program that downloads pictures off the internet and then displays it with pygtk. The problem is that in order to do this and keep the GUI responsive, I need to use threads. So I got into a callback after the user clicked on the button "Download pictures" and I call the method to download the pictures that is within that same class. thread...

python - GNOME applet using threads hangs

I am trying to develop a GNOME applet (put into panel) using python (pyGTK). I've started by following the tutorial suggested in other SO question. My plan is to let the applet do something in the background in a repetitive fashion (causing its display to be updated). So I thou...

python - Should I use fork or threads?

In my script, I have a function foo which basically uses pynotify to notify user about something repeatedly after a time interval say 15 minutes. def foo: while True: """Does something""" time.sleep(900) My main script has to interact with user & does all other things so I just cant call the foo() function. directly.

python - Who stops my threads?

I have some threads fishing into a queue for jobs, something like this: class Worker(Thread): [...] def run(self): while not self.terminated: job = myQueue.get_nowait() job.dosomething() sleep(0.5) Now, self.terminated is just a bool value I use to exit the loop but, this is the problem, several times in a day they stop working without my...

python threads & sockets

I have a "I just want to understand it" question.. first, I'm using python 2.6.5 on Ubuntu. So.. threads in python (via thread module) are only "threads", and is just tells the GIL to run code blocks from each "thread" in a certain period of time and so and so.. and there aren't actually real threads here.. So the question is - if I have a blocking socket in one thread, and now I'm sending data and block th...

Threads in Python again

guys! My application is a bot. It simply receives a message, process it and returns result. But there are a lot of messages and I'm creating separate thread for processing each, but it makes an application slower (not a bit). So, Is it any way to reduce CPU usage by replacing threads with something else?

python - How can I modify a file while other threads read from it?

I'm multithreading data (both reading and writing) into/from a single text file. I need to be able to replace or remove text from this file without replacing the actual file. I cannot simply .read() into a string, then .write() a new file with the same name, because other threads are still using that file. I cannot .write() into a different file because threa...

python - Why should you lock threads?

I've read a lot of examples on locking threads.. but why should you lock them? From my understanding, when you initiate threads without joining them, they will compete with the main thread and all other threads for resources and then execute, sometimes simultaneously, sometimes not. Does locking ensure that threads DON'T execute simultaneously? Also, what wrong with threads executing simultaneous? Isn't tha...

Python threads in C

I'am writing a multi-threaded program in C. Before creating the threads, a global python environment is initialized by calling Py_Initialize(). Then, in every created thread, the global python environment is shared, and each thread calls a python method with the parameters converted in C. Everything works well un...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python